Reputation: 161
I've tried to using a SimpleAdapter to fill a ListView but only to failed.
I've read many cases but still can't find a useful method.
Here is the Code. I wonder what's the problem.
ArrayList<HashMap<String,Object>> listdata=new ArrayList<HashMap<String,Object>>();
for (int i=0;i<3;i++){
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("title", "title");
hm.put("context", "context");
listdata.add(hm);
}
String[] from = {"title", "context"};
int[] to={R.id.title,R.id.context};
SimpleAdapter adapter = new SimpleAdapter(this, listdata, R.layout.list_item, from, to);
ListView lv=(ListView)this.findViewById(R.id.listView1);
lv.setAdapter(adapter);
//code for the list_item
<LinearLayout
<ListView android:layout_height="wrap_content" android:id="@+id/listV1" android:layout_width="match_parent">
<LinearLayout>
<TextView android:layout_height="wrap_content" android:id="@+id/title" android:text="TextView" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<TextView android:text="TextView" android:id="@+id/context" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</ListView>
I do appreciate if anyone can help!
Upvotes: 3
Views: 4910
Reputation: 4753
I suspect the problem is with your Xml, try declaring a second layout file for the list item. e.g.
your_activity.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_width="fill_parent" />
</LinearLayout>
and list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="wrap_content" android:id="@+id/title" android:text="TextView" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<TextView android:text="TextView" android:id="@+id/context" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
Your code looks ok and should work correctly if you use the above two layout files.
Upvotes: 3
Reputation: 9242
I have always put my list items in a seperate xml file. Maybe try putting your LinearLayout with the two TextViews in a file (maybe call it listrow.xml).
As an example:
Here is the listitem (list_stat_item.xml):
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="" android:id="@+id/txtKey"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="10dp" android:textSize="16sp"></TextView>
<TextView android:text="" android:id="@+id/txtValue"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="10dp" android:textSize="16sp"></TextView>
</LinearLayout>
And here is the main view (view_stats.xml):
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1"
android:transcriptMode="normal" android:divider="@drawable/list_divider"
android:dividerHeight="1px" />
</LinearLayout>
And here is the code:
Create an ArrayList, and add items to it, then bind to a custom adapter.
ArrayList<StatItem> lst = new ArrayList<StatItem>();
lst.add(new StatItem("Current Week: ", String.valueOf(currentWeek)));
lst.add(new StatItem("Weeks Left: ", String.valueOf(GeneralUtils.daysUntilBirth(this, pregnancyID) / 7)));
lst.add(new StatItem("Days Left: ", String.valueOf(GeneralUtils.daysUntilBirth(this, pregnancyID))));
lst.add(new StatItem("Current Trimester: ", String.valueOf(currentTrimester)));
lst.add(new StatItem("Posts: ", String.valueOf(blogCount)));
lst.add(new StatItem("Baby Names: ", String.valueOf(babyNamesCount)));
getListView().setSelector(android.R.color.transparent);
StatListAdapter sa = new StatListAdapter(this, R.layout.list_stat_item, lst );
setListAdapter(sa);
If you want to see the adapter code (StatListAdapter extends ArrayAdapter):
public class StatListAdapter extends ArrayAdapter<StatItem>
{
public ArrayList<StatItem> items;
public StatListAdapter(Context context, int textViewResourceId, ArrayList<StatItem> objects) {
super(context, textViewResourceId, objects);
this.items = objects;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.list_stat_item, null);
}
StatItem i = items.get(position);
if (i != null)
{
TextView kt = (TextView)v.findViewById(R.id.txtKey);
TextView vt = (TextView)v.findViewById(R.id.txtValue);
kt.setText(i.key);
vt.setText(i.value);
}
return v;
}
}
Upvotes: 1