2011-09-21 17 views
0

我有4個標籤A,B,C和D的每個選項卡中有一個列表視圖,其我使用以下代碼來填充:加入列表視圖的圖像,列表視圖存在於tabwidget

TabSpec s1=tabhost1.newTabSpec("Tab A"); 
s1.setIndicator("A",getResources().getDrawable(R.drawable.A)); 
s1.setContent(R.id.listV1); 
tabhost1.addTab(s1); 

listview1= (ListView) findViewById(R.id.listV1); 
listview1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.tab1content))); 

現在我想爲listview中的每個項目添加一個圖像,任何導致我如何去做這件事?

+0

顯示您自定義的getView方法您正在使用的適配器將行設置爲圖像。 – blessenm

回答

0

這個工作對我來說這個名單:

class ListAdapter extends BaseAdapter { 

    public int getCount() { //returns the size of your list 
     if (imgDetails != null) { 
      return imgDetails.size(); 
     } 
     return 0; 
    } 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
    public View getView(int arg0, View convertView, ViewGroup arg2) { 
     if (convertView == null) { 
      convertView = getLayoutInflater().inflate(R.layout.details_cell_view, null); // create an xml to inflate and put an imageview , textview in it. 
     } 
     String str = imgDetails.get(arg0).getId(); 
     if(str.equals("1")) 
      ((ImageView) convertView.findViewById(R.id.img_details_img)).setBackgroundResource(R.drawable.img_1); 
     if(str.equals("2")) 
      ((ImageView) convertView.findViewById(R.id.img_details_img)).setBackgroundResource(R.drawable.img_2); 
     if(str.equals("3")) 
      ((ImageView) convertView.findViewById(R.id.img_details_img)).setBackgroundResource(R.drawable.img_3); 
     if(str.equals("4")) 
      ((ImageView) convertView.findViewById(R.id.img_details_img)).setBackgroundResource(R.drawable.img_4); 


     ((TextView) convertView.findViewById(R.id.img_details_txt)).setText(imgDetails.get(arg0).getText()); 
     //to set an arrow at the right of each row 
     ((ImageView) convertView.findViewById(R.id.right_arrow)).setBackgroundResource(R.drawable.arrow_img); 
     convertView.setPadding(1, 1, 1, 0); 
     return convertView; 
    } 

} 
0

創建自定義ListAdapter,您將在其中設置圖像以列出項目。設置此適配器作爲ListAdapter使用

ListView listView = (ListView) findViewById(R.id.listV1); 
listView.setAdapter(new CustomListAdapter(this)); 

檢查本教程如何創建自定義適配器 http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

+0

我曾嘗試過。我面臨的問題是,我得到了相同的圖像在列表視圖中的所有行。如何更改每行的圖像? – user838522

+0

這將基於你如何在ListAdapter中綁定你的視圖。如果你正確地約束你的觀點,這應該不成問題。也許你可以共享你的ListAdapter的代碼 –

+0

listview2 =(ListView)findViewById(R.id.listV2); (新的ArrayAdapter (this,android.R.layout.simple_list_item_1,getResources()。getStringArray(R.array.tab2content))); 我面對的另一個問題是,我有4個不同的ListViews(每個標籤一個),所以我不能在我的項目中使用相同的id:「@ android:id/list」因爲我認爲它會在R.java文件中發生衝突。 – user838522