2011-12-18 13 views
6

He estado jugando con la actividad tutorial lista aquí:elemento de la lista personalizada a Listview androide

http://developer.android.com/resources/tutorials/views/hello-listview.html

que le dice al comenzar la actividad se extiende lista.

by public class Main extends ListActivity { 

que se basa en inflar un TextView única disposición.

<?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> 

Si quiero personalizar el diseño más mediante la adición de imágenes y un diseño lineal adicional por encima del adaptador lista etc- es posible usar este método- si cómo lo hago?

Respuesta

15

Es posible usando un SimpleAdapter.

Aquí es un ejemplo:

// Create the item mapping 
    String[] from = new String[] { "title", "description" }; 
    int[] to = new int[] { R.id.title, R.id.description }; 

Ahora "título" se asigna a R.id.title, y "descripción" para R.id.description (definido en el XML a continuación).

// 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); 

Ésta es la disposición XML correspondiente, aquí llamado 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> 

que utiliza dos TextViews pero funciona lo mismo con cualquier tipo de vista.

Cuestiones relacionadas