2011-12-18 36 views
6

मैं सूची गतिविधि ट्यूटोरियल यहाँ के साथ खेल रहे हैं:कस्टम सूची आइटम ListView के लिए एंड्रॉयड

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

जिससे आपको पता चलता सूची गतिविधि का विस्तार शुरू करने के लिए।

by public class Main extends ListActivity { 

जो टेक्स्टव्यू केवल लेआउट को बढ़ाने पर आधारित है।

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

मैं चित्र जोड़कर लेआउट अधिक अनुकूलित करने के लिए चाहते हैं, और सूची एडाप्टर ऊपर एक अतिरिक्त रेखीय लेआउट आदि-यह संभव है इस method- का उपयोग कर यदि ऐसा है तो मैं इसे कैसे करते हैं?

उत्तर

15

SimpleAdapter का उपयोग करके यह संभव है।

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

अब "शीर्षक" R.id.title को मैप किया है, और R.id.description (नीचे एक्सएमएल में परिभाषित) को "description":

यहाँ एक उदाहरण है।

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

यह इसी एक्सएमएल लेआउट, यहाँ 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> 

मैं दो TextViews इस्तेमाल किया, लेकिन यह दृश्य के किसी भी प्रकार के साथ एक ही काम करता है।

संबंधित मुद्दे