2010-08-11 59 views
0

我想創建一個ListView,其中包含一個包含ImageView和另一個佈局(線性)的RelativeLayout。線性佈局包含一些TextView。創建包含佈局的ListView

如何創建此ListView?

感謝 迪帕克

+0

你的意思是每一行都是你描述的RelativeLayout?如果是這樣,那很簡單。你有沒有看過'ListAdapter'和'BaseAdapter'類? – 2010-08-11 07:29:52

回答

0

您可以爲各行的佈局。我們假設ImageView是R.id.image和TextView R.id.text。行佈局是R.id.row_layout 對於列表,您需要一個列表適配器。如果使用ListActivity它可能是這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    ... 
    ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>(); 
    HashMap<String, Object> m = new HashMap<String, Object>(); 
    m.put("image", R.drawable.some_image); 
    m.put("text", "Test"); 
listData.add(m); 
    adapter = new SimpleAdapter(this, 
      listData, R.layout.row_layout, 
     new String[] {"image", "text"}, 
     new int[] {R.id.image, R.id.text}); 
    setListAdapter(adapter); 
} 

ArrayList中包含列表中的所有數據。每一行都用一個HashMap表示,數據的鍵必須等於你想要顯示它們的ID。在適配器中,您說關鍵圖像映射到ImageView等等。