I have already created a dynamic list view that passes text to a SimpleAdapter, however I would like to pass information to make an image appear from the drawable folder. Here is the XML layout for each row:
<ImageView
android:id="@+id/sport_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<TextView
android:id="@+id/item_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
and the bit of code creating the list:
//fill in the list items from the XML document
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "fixture_id"));
map.put("sport", XMLfunctions.getValue(e, "sport"));
map.put("teams", XMLfunctions.getValue(e, "team_2") + " v " + XMLfunctions.getValue(e, "team_2"));
mylist.add(map);
}
//Make a new listadapter
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.fixture, new String[] { "sport", "teams" }, new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
Now at the moment when I pass the string sport it just displays the text such as ‘football’ in the item_title text view. But as I know what the sport is I would like to be able to display an icon in the sport_icon imageview. An example of the sport icon location would be @drawable/icon_football.
Many thanks.
The answer here was creating a BaseAdapter with the ListView as suggested by Frank below. If anyone else is looking to do the same thing but has no idea where to start I highly recommend watching http://www.youtube.com/watch?v=pVs4qKmenQM. This video is well put together and explains everything you need to know.
🙂
You are going to have to extend something like BaseAdapter and check what your position is in getView() and then accordingly set what image you would like to use.
SimpleAdapter only works nice if ALL the items are have the exact same format and it’s very simple.
Here is an ok example.
http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/
But I would really recommend watching the Romain and Adam’s Google IO Video
http://www.youtube.com/watch?v=wDBM6wVEO70