I’m starting to work with the Android LayoutInflater and I need some help.
I have a ListActivity which uses a layout defined in the following 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:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
This ListView is filled with an ArrayAdapter which generates each row by inflating another XML like this:
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/item_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="20dip"
android:textColor="#FFF"
/>
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="6dip"
android:src="@drawable/example_icon"
/>
</LinearLayout>
</LinearLayout>
This is working fine, but now I want to show a variable number of icons for each item (so, each row of the ListView could have a different number of ImageViews under the TextView).
Maybe I have to define another XML for the list of icons and somehow build the ListView from nested layouts? I don’t know what’s the better way to do this. Any suggestions?
Thank you!
Edit: This is an example of what im trying to do: http://tinypic.com/r/2s9yniw/7
You’ll just have to include a number of images in the XML layout for each row, and set the visibility of these to
GONEwhen you no longer need them (you could put an image in one layout XML file and thenincludethis 3 times on your row layout file).You might be able to do it by inflating the image layout XML file as many times as you need for each row then adding it to the row view (but this gives problems with recycling if you only added 2 images the first time and the next row needs 3); but from what I remember you cannot add inflated content to an Adapter row or you get an exception (not entirely sure, so give it a try if in doubt).