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!
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.