6

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 activity.

by public class Main extends ListActivity {

Which is based on inflating a textview only layout.

   <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

If I want to customize the layout more by adding images,and an extra linear layout above the list adapter etc- is it possible using this method- if so how do I do it?

1 Answer 1

16

It is possible by using a SimpleAdapter.

Here is an example :

    // Create the item mapping
    String[] from = new String[] { "title", "description" };
    int[] to = new int[] { 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 rows
    List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();

    HashMap<String, Object> map = new HashMap<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 = new HashMap<String, Object>();
    map.put("title", "Second title");
    map.put("description", "description 2");
    fillMaps.add(map);

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

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android: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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.