Mustard
Mustard

Reputation: 1840

Multiple icons in Android ListView

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?

Upvotes: 2

Views: 1637

Answers (2)

Mahesh
Mahesh

Reputation: 2892

Or you can create custom listview using base adapter

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006604

With an ArrayAdapter, you would need to override getView() 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 the ImageView in the row and set its image based on the position parameter supplied to getView(). Return the resulting row.

Here's a free excerpt from one of my books that covers much of this.

Upvotes: 3

Related Questions