2011-12-18 33 views
6

Tôi đã được chơi với danh sách các hoạt động hướng dẫn ở đây:danh mục Custom để ListView android

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

mà nói với bạn để bắt đầu mở rộng hoạt động Danh sách.

by public class Main extends ListActivity { 

Tùy thuộc vào việc tăng bố cục chỉ xem văn bả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> 

Nếu tôi muốn tùy chỉnh bố cục hơn bằng cách thêm hình ảnh, và một bố trí tuyến tính thêm trên adapter danh sách etc- là nó có thể sử dụng phương pháp luận này nếu có thì làm thế nào để làm điều đó?

Trả lời

15

Có thể sử dụng SimpleAdapter.

Dưới đây là một ví dụ:

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

Now "danh hiệu" được ánh xạ tới R.id.title, và "mô tả" để R.id.description (được định nghĩa trong XML dưới đây).

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

Đây là cách bố trí XML tương ứng, ở đây tên là 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> 

tôi đã sử dụng hai TextView nhưng nó hoạt động giống với bất kỳ loại xem.

Các vấn đề liên quan