Skip to content Skip to sidebar Skip to footer

Custom List Item To Listview Android

I have been playing with the list activity tutorial here: http://developer.android.com/resources/tutorials/views/hello-listview.html which tells you to start off extending List ac

Solution 1:

It is possible by using a SimpleAdapter.

Here is an example :

// Create the item mappingString[] from = newString[] { "title", "description" };
    int[] to = newint[] { R.id.title, R.id.description };

Now "title" is mapped to R.id.title, and "description" to R.id.description (defined in the XML below).

// Add some rowsList<HashMap<String, Object>> fillMaps = newArrayList<HashMap<String, Object>>();

    HashMap<String, Object> map = newHashMap<String, Object>();
    map.put("title", "First title"); // This will be shown in R.id.title
    map.put("description", "description 1"); // And this in R.id.description
    fillMaps.add(map);

    map = newHashMap<String, Object>();
    map.put("title", "Second title");
    map.put("description", "description 2");
    fillMaps.add(map);

    SimpleAdapter adapter = newSimpleAdapter(this, fillMaps, R.layout.row, from, to);
    setListAdapter(adapter);

This is the corresponding XML layout, here named row.xml :

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/title"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceMedium" /><TextViewandroid:id="@+id/description"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceSmall" /></LinearLayout>

I used two TextViews but it works the same with any kind of view.

Post a Comment for "Custom List Item To Listview Android"