I want to remove any space between different items in a ListView.
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/wrapper"
android:padding="0dp"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:textColor="#000000"/>
</LinearLayout>
And Listview
<ListView
android:id="#+id/listView1"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
android:dividerHeight="0dp"
android:divider="#null"
android:listSelector="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
But there is still some space between the items. Can anyone help me?
The line android:dividerHeight="10dp" causes the gaps between your lines. I color-coded the overall UI:
Once I set the dividerHeight line above from "10dp" to "0dp" I got this:
Ok, so here is the full set of code I used so you can see where you may have gone wrong.
MainActivity:
package com.ds.listviewtest;
import android.app.ListActivity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
static final String[] FRUITS = new String[] {
"Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new SimpleAdapter(FRUITS));
setContentView(R.layout.activity_main);
}
private class SimpleAdapter implements ListAdapter
{
String[] items;
public SimpleAdapter(String[] items)
{
this.items = items;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.comment);
textView.setText(items[position]);
return rowView;
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
#Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return true;
}
}
}
Here is list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#android:color/holo_orange_dark"
android:id="#+id/wrapper"
android:padding="0dp"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/comment"
android:background="#android:color/holo_blue_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:text="This is a test comment"
android:textColor="#000000"/>
</LinearLayout>
Finally here is activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/holo_green_light"
android:dividerHeight="0dp" /> <!-- EDIT THIS VALUE HERE TO 0DP -->
</RelativeLayout>
Simply, give your divider height a negative value.
Example :
android:dividerHeight="-20dp"
This will remove spaces between ListView values.
Related
I'm trying to make custom adapter for my small app, I got stuck in filling my rows with data, I can't find a good tutorial for that. I know how to use Simple Adapter and Array Adapter, but custom ones have some trouble.
I don't know how to implement it, and the online articles confused me, Please help me with the logic and what steps should I do implement.
package com.example.administrator.healthyfood;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] food = {"Dog","Cat","Cow","Fish","Frog","Bird","Rabbit","Horse","Chikcen"};
#Override
protected void onCreate(Bundle savedInstanceState) {
list = (ListView)findViewById(R.id.listView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class MyListViewAdapter extends BaseAdapter {
#Override
public int getCount() {
return food.lenght();
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
return null;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="255dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#339966"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video1"
android:textColor="#606060" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
Your MainActivity.java -
package com.example.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
}
Hope this is enough for you.
You can create a simple Adapter class using these simple steps
make a java class
public class Wallet_Adapter extends RecyclerView.Adapter<Wallet_Adapter.ViewHolder>
{
LayoutInflater inflater;
List<Wallet_Model> modelclasslists;
public Wallet_Adapter(Context ctx, List<Wallet_Model> modelclasslists)
{
this.inflater=LayoutInflater.from(ctx);
this.modelclasslists=modelclasslists;
}
#NonNull
#Override
public Wallet_Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.layout_wallet,parent,false);
return new Wallet_Adapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Wallet_Adapter.ViewHolder holder, int position) {
holder.date.setText(modelclasslists.get(position).getDate());
holder.amount.setText(modelclasslists.get(position).getAmount());
holder.description.setText(modelclasslists.get(position).getDescription());
holder.type.setText(modelclasslists.get(position).getType());
}
#Override
public int getItemCount() {return modelclasslists.size(); }
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView date,amount,description,type;
public ViewHolder(#NonNull View itemView)
{
super(itemView);
date=itemView.findViewById(R.id.dateans);
amount=itemView.findViewById(R.id.amountans);
description=itemView.findViewById(R.id.descriptionans);
type=itemView.findViewById(R.id.typeans);
}
}
}
AutoSearchDetails.java
fetching value from mysql to create a listview although am a beginner so please help me
protected void onPostExecute(String result) {
Log.d("jason got", result);
JSONArray jarray;
try {
jarray = new JSONArray(result);
for(int i=0;i<jarray.length();i++){
HashMap<String, String> hm;
hm=new HashMap<String, String>();
JSONObject jobj=jarray.getJSONObject(i);
hm.put("Name",jobj.getString("name"));
hm.put("Phone",jobj.getString("phone"));
Log.d("name", jobj.getString("name"));
oslist.add(hm);
}
} catch (JSONException e) {
Toast.makeText(getBaseContext(), "Not getting result", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Log.d("oslist", oslist.toString());
ListAdapter adapter = new SimpleAdapter(AutoSearchDetails.this, oslist,R.layout.listitem,new String[] { Name,Phone }, new int[] {
R.id.name,R.id.phone});
Log.d("adapter", adapter.toString());
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(
AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(AutoSearchDetails.this, "You Clicked at "+oslist.get(+arg2).get("name"), Toast.LENGTH_SHORT).show();
}
});
};
in log i do get "oslist" [{"name:fe","phone:32"}]
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
The problem is the value is not setting to the listview
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Map;
public class MyAdapter extends BaseAdapter {
private final ArrayList mData;
public MyAdapter(Map<String, String> map) {
mData = new ArrayList();
mData.addAll(map.entrySet());
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Map.Entry<String, String> getItem(int position) {
return (Map.Entry) mData.get(position);
}
#Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_adapter_item, parent, false);
} else {
result = convertView;
}
Map.Entry<String, String> item = getItem(position);
// TODO replace findViewById by ViewHolder
((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());
((TextView) result.findViewById(android.R.id.text2)).setText(item.getValue());
return result;
}
}
layout for this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
And adapter in your code
public void showCinemas(HashMap<String, String> cinemas) {
MyAdapter adapter = new MyAdapter(cinemas);
list.setAdapter(adapter);
}
I'm trying to create an android widget containing a list of elements,but when drag the widget on home screen i get the message "loading" instead of the name of the item i want to display, What is the problem?
this is my list provider:
package com.example.noteskeeper;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ListView;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService.RemoteViewsFactory;
import android.appwidget.AppWidgetManager;
public class ListProvider implements RemoteViewsFactory {
private Context context;
private int appWidgetId;
//private static final int mCount = 10;
//Context context
//private DBTools dbTools = new DBTools(context);
ArrayList<HashMap<String,String>> lists;
public ListProvider( Context context, Intent intent,ArrayList<HashMap<String,String>> listItems ){
this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
lists = listItems;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return lists.size();
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return Long.parseLong(lists.get(position).get("noteId"));
}
#Override
public RemoteViews getLoadingView() {
// TODO Auto-generated method stub
return null;
}
#Override
public RemoteViews getViewAt(int position) {
// TODO Auto-generated method stub
RemoteViews remoteView = new RemoteViews(
context.getPackageName(), R.layout.note_entry);
//HashMap<String,String> row = lists.get(position);
remoteView.setTextViewText(R.id.tvNoteId, lists.get(position).get("noteId"));
remoteView.setTextViewText(R.id.tvNoteTitle, lists.get(position).get("noteTitle"));
return remoteView;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
}
#Override
public void onDataSetChanged() {
// TODO Auto-generated method stub
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
}
}
this is my service:
package com.example.noteskeeper;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
public class NotesWidgetService extends RemoteViewsService {
DBTools dbTools = new DBTools(this);
#Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
// TODO Auto-generated method stub
return (new ListProvider(this.getApplicationContext(), intent,
dbTools.getNotes()));
}
}
this is my provider:
package com.example.noteskeeper;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.ListAdapter;
import android.widget.RemoteViews;
import android.widget.SimpleAdapter;
public class NotesAppWidgetProvider extends AppWidgetProvider {
#SuppressWarnings("deprecation")
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.note_widget);
for (int i=0; i<N; i++) {
Intent intent = new Intent(context, NotesEditor.class);
Intent intent2 = new Intent(context, NotesFirst.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, 0);
views.setOnClickPendingIntent(R.id.btn_wnew, pendingIntent);
views.setOnClickPendingIntent(R.id.textView1, pendingIntent2);
Intent svcIntent = new Intent(context, NotesWidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
svcIntent.setData(Uri.parse(
svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(appWidgetIds[i],R.id.noteslist,
svcIntent);
views.setEmptyView(R.id.noteslist, R.id.empty_view);
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
}
}
}
my xml files:
widget provider:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dip"
android:minHeight="110dip"
android:widgetCategory="home_screen|keyguard"
android:initialLayout="#layout/note_widget"
android:resizeMode="horizontal|vertical" android:updatePeriodMillis="1800000">
</appwidget-provider>
widget xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="#color/white">
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/app_name"
android:textColor="#color/white"
android:background="#color/black"
android:textAlignment="center"
android:gravity="center"
android:layout_marginRight="0.1dp"
/>
<Button
android:id="#+id/btn_wnew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/as"
android:background="#color/black"
android:textSize="26sp"
style="?android:attr/borderlessButtonStyle"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/empty_string"
android:textColor="#ffffff"
android:textSize="20sp"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list" >
<ListView
android:id="#+id/noteslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium"
>
</ListView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
my element xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvNoteId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/tvNoteTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
</TableRow>
</TableLayout>
I have an custom ExpandableListView Adapter:
public class MyExpandableListView extends BaseExpandableListAdapter {
private Context context;
private List<? extends List<? extends Map<String, ?>>> mChildData;
private List<? extends Map<String, ?>> mGroupData;
public MyExpandableListView(Context context,List<? extends Map<String, ?>> GroupData,
List<? extends List<? extends Map<String, ?>>> ChildData){
this.context = context;
this.mChildData=ChildData;
this.mGroupData=GroupData;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return mChildData.get(groupPosition).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_list_layout, null);
}
TextView childtxt = (TextView) convertView.findViewById(R.id.textView1);
childtxt.setText((CharSequence) mChildData.get(groupPosition).get(childPosition).get("body"));
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return mChildData.get(groupPosition).size();
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return mGroupData.get(groupPosition);
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return mGroupData.size();
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_list_layout, null);
}
TextView time = (TextView)convertView.findViewById(R.id.item1);
TextView title = (TextView)convertView.findViewById(R.id.item2);
time.setText((CharSequence) mGroupData.get(groupPosition).get("time"));
title.setText((CharSequence) mGroupData.get(groupPosition).get("titel"));
View view2 = (View)convertView.findViewById(R.id.view2);
view2.bringToFront();
return convertView;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
My group_list_layout.xml is following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<View
android:id="#+id/view2"
android:layout_width="2dp"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:background="#ff000000" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="row_id"
android:textSize="18sp"
android:width="100dip" />
<TextView
android:id="#+id/item2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="col_1"
android:textSize="18sp"
android:width="300dip" />
</LinearLayout>
</RelativeLayout>
I am trying to display a vertical line in front of the "time" of the group element.
But my view (view2) isn't displayed. Why?
Your LinearLayout is positioned on top of the View element. Instead move the View element after the LinearLayout in the group layout so it will be placed above it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="row_id"
android:textSize="18sp"
android:width="100dip" />
<TextView
android:id="#+id/item2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="col_1"
android:textSize="18sp"
android:width="300dip" />
</LinearLayout>
<View
android:id="#+id/view2"
android:layout_width="2dp"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:background="#ff000000" />
</RelativeLayout>
Also, inflate your layout like this:
convertView = infalInflater.inflate(R.layout.group_list_layout, parent, false);
As it is a Relative layout adding android:layout_toLeftOf="#id/view2" to the LinearLayout should work.
I have a list view that looks like this
and that's its code ..
package com.a.c;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class calender extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, calendr));
getListView().setTextFilterEnabled(true);
}
static final String[] calendr = new String[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"Rotring",
"Sheaffer",
"Waterman"
};}
i want to modify it to add this layout ,, and shift the listview downward little bit
the image looks like a picture frame that's why i want to move the list view ..
Layout code at .XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/relativeLayout1"
android:layout_width="600px"
android:layout_height="1024px"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="#+id/layout"
android:src="#drawable/layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"></ImageView>
</RelativeLayout>
Hi You can use this code and achieve your target:
main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/layout"
android:src="#drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
<ListView
android:layout_width="match_parent"
android:id="#+id/listViewScore"
android:cacheColorHint="#00000000"
android:layout_height="match_parent"
android:layout_weight="1.00"
android:divider="#C0C0C0"
android:layout_below="#+id/layout"
android:dividerHeight="2dip" />
</RelativeLayout>
listviewtext.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Name"
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"></TextView>
</LinearLayout>
public class TestProjeectActivity extends Activity {
private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
private String[] usernameArr = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listViewScore=(ListView)findViewById(R.id.listViewScore);
usernameArr = new String[]{"Alan","Bob","Carl","Doaniel","Evan","Fred","Gram","Ian","Jordan"};
listViewAdapter = new ListViewAdapter();
listViewScore.setAdapter(listViewAdapter);
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(usernameArr==null){
return 0;
}
return usernameArr.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return usernameArr[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate =LayoutInflater.from(TestProjeectActivity.this);
rowView=layoutinflate.inflate(R.layout.listviewtext, parent, false);
}
TextView textViewName=(TextView)rowView.findViewById(R.id.textViewName);
textViewName.setText(usernameArr[position]);
return rowView;
}
}
}