Note that my activity is NOT extending ListActivity; just Activity.
I have a list view with a custom layout as follows:
(res/layout/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:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/icon"
android:layout_width="30dip"
android:paddingTop="15dip"
android:layout_height="wrap_content"
android:src="@drawable/icon_default"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textColor="#ffffff"
/>
</LinearLayout>
I’m then setting the adapter for the ListView to an ArrayAdapter using an ArrayList, here called ‘machines’.
ListView rooms = (ListView)findViewById(R.id.machines);
ArrayAdapter<String> aa = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, android.R.id.text1, machines);
rooms.setAdapter(aa);
How would I go about doing a similar thing for the ImageView field?
With an
ArrayAdapter, you would need to overridegetView()and do more of the work yourself. You can either inflate the row yourself (getLayoutInflater().inflate()) or chain to the superclass to get your row. Then, find theImageViewin the row and set its image based on the position parameter supplied togetView(). Return the resulting row.Here’s a free excerpt from one of my books that covers much of this.