2011-12-18 19 views
6

Gram z samouczka działalności lista tutaj:Niestandardowy element listy do ListView android

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

który każe ci rozpocząć działalność rozszerzenie listy.

by public class Main extends ListActivity { 

Co jest oparte na nadmuchiwaniu układu tylko w trybie tekstowym.

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

Jeśli chcę dostosować układ bardziej poprzez dodanie zdjęć, a dodatkowy układ liniowy powyżej adaptera lista itp. jest to możliwe przy użyciu tego method- jeśli tak, w jaki sposób mogę to zrobić?

Odpowiedz

15

Jest to możliwe przy użyciu SimpleAdapter.

Oto przykład:

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

Teraz "title" jest odwzorowywany R.id.title i "opis", aby R.id.description (zdefiniowanego w poniższej XML).

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

Jest to odpowiedni układ XML, tutaj nazwany 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> 

Użyłem dwóch TextView ale działa tak samo z każdego rodzaju widoku.

Powiązane problemy