How can I set drawable to a ListView in android
Posted
by sxingfeng
on Stack Overflow
See other posts from Stack Overflow
or by sxingfeng
Published on 2010-04-18T15:25:59Z
Indexed on
2010/04/18
15:33 UTC
Read the original article
Hit count: 371
I am writing a app for android 1.5. I want to use a complex listview to display my data. I want to show a ImageView of a drawable object in my List item.
I learned from a demo:
------> listData.put("Img", listData.put("Img", R.drawable.XXX));
listData.put("Time", "100");
listItems.add(listData);
It can display correctly, however, I want to change Img at runtime, The image maybe generated at run-time, so I change the code as follow, but it falls. Can anyone help me ? many thanks!
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.item_list);
itemListView = (ListView) findViewById(R.id.listview);
ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
for(int i = 0;i <XXX.size(); ++i)
{
HashMap<String, Object> listData = new HashMap<String, Object>();
---------> 1) listData.put("Img", new Drawable(XXX));
2) listData.put("Time", "100");
3) listItems.add(listData);
}
SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItems,
R.layout.listitem,
new String[] { "Img", "Time"},
new int[] { R.id.listitem_img, R.id.listitem_time });
itemListView.setAdapter(listItemAdapter);
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<ImageView
android:paddingTop="12dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listitem_img" />
<TextView
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_width="wrap_content"
android:id="@+id/listitem_time" />
</LinearLayout>
© Stack Overflow or respective owner