2011-03-07 25 views
1

我試着做一個列表框,每個列出的項目有不同的圖像和文本。我試圖創建兩個setListAdapter,一個用於顯示文本字符串,另一個用於圖像名稱。它沒有工作,我假設有另一種方法來做到這一點。Android的listview,想在我的列表視圖的每一行有不同的圖片

我CTICTY類的名單

public class cFashions extends ListActivity { 
    TextView selection; 
    // list to fill list box wi*/ 
/* 
    String[] items={"CASUAL","DR ESSES","CAREER","OUTWEAR","FOOTWEAR", 
      "JEWELRY","ACCESSORIES"}; 
*/ 
    String[] items={"CASUAL"}; 
    String[] icons={"icon"}; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fashions); 

     setListAdapter(new ArrayAdapter<String>(this, 
      R.layout.row, R.id.label, 
      items)); 

     setListAdapter(new ArrayAdapter<String>(this, 
      R.layout.row, R.id.icon, 
      icons)); 
      selection=(TextView)findViewById(R.id.selection); 
    } 
} 

爲每個單元的佈局XML文件中的列表

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
> 

<ImageView 
     android:id="@+id/icon" 
     android:layout_width="22px" 
     android:paddingLeft="2px" 
     android:paddingRight="2px" 
     android:paddingTop="2px" 
     android:layout_height="wrap_content" 

    /> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="12sp" 
    /> 
</LinearLayout> 

特德

回答

5

你需要使用一個適配器,而不是兩個,該適配器知道如何處理您的TextView和您的ImageViewHere is a free excerpt來自我的一本書,詳細介紹了這個主題。

+0

不錯的作品CommonsWare – BonanzaDriver 2011-03-07 04:54:22

+0

它非常有用的CommonsWare – 2012-07-05 09:51:03

相關問題