2014-04-08 149 views
1

我需要使用替代顏色製作ListView。ListView替代的顏色

代碼:

public View getView(int position, View convertView, ViewGroup parent) { 

    if(position %2==0) 
    { 
     view.setBackgroundColor(Color.BLUE); 
    } 
    return view; 
} 

,但我不知道如何使用它在我的適配器:

SimpleAdapter adapter = new SimpleAdapter(this, propositions, android.R.layout.simple_list_item_2, 
    new String[] {"Date", "Trajet"}, 
    new int[] { android.R.id.text1, 
    android.R.id.text2}); 
lvTrajets.setAdapter(adapter); 

有人可以幫助我瞭解如何做到這一點嗎?

+0

只是覆蓋getView您的適配器 – Raghunandan

+0

你必須寫自己的適配器,並覆蓋getView –

+0

我怎麼能這樣做? – user3507621

回答

2

覆蓋getView用於適配器

SimpleAdapter adapter = new SimpleAdapter(this, propositions, android.R.layout.simple_list_item_2, 
new String[] {"Date", "Trajet"}, 
new int[] { android.R.id.text1, 
android.R.id.text2}) 
    { 
     @Override 
     public View getView (int position, View convertView, ViewGroup parent) 
     { 
      View v = super.getView(position, convertView, parent); 
      if(position %2==0) 
      { 
        v.setBackgroundColor(Color.BLUE); 
      } 
      else 
      { 
        v.setBackgroundColor(Color.WHITE); 
      } 

      return v; 
     } 


    }; 
+0

它使我的應用程序崩潰 – user3507621

+0

@ user3507621沒有它應該工作。發佈stacktrace – Raghunandan

+0

@ user3507621你可以檢查類似的帖子http://stackoverflow.com/questions/22905041/why-cant-i-click-a-button-in-a-listview-using-simpleadapter-in-android/ 22905116#22905116但沒有設置背景 – Raghunandan

0

這是我獲取視圖我改變一個圖像的背景你剛剛更改父佈局顏色特定位置弗朗你的邏輯,如果(在位置%2 == 0)這裏簡單,我只給ü欺騙了這傢伙不完整的代碼

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View v = convertView; 

     if (v == null) { 
      v = mInflater.inflate(R.layout.devicesnameshow_item, null); 
     } 


     ImageView imgIcon = (ImageView) v.findViewById(R.id.deviceimage); 

     Resources res = v.getResources(); 
     String mDrawableName = data[position].umt; 
     int resID = res.getIdentifier(mDrawableName , "drawable", context.getPackageName()); 
     Drawable drawable = res.getDrawable(resID); 
     imgIcon.setImageDrawable(drawable); 




      if(position %2==0) 
     { 
      imgIcon.setBackgroundColor(Color.parseColor(colorcounter_array[colorcounter]));// u can give any color i just take it from my array 
     } else { 
      imgIcon.setBackgroundColor(Color.parseColor(colorcounter_array[colorcounter]));// u can give any color i just take it from my array 
     } 




     } 
     return v; 
    } 
0

對於表示ListView替代顏色以如下方式實施你的AdaptergetView()方法

public View getView(int position, View convertView, ViewGroup parent) { 

if(convertView==null){ 
//Initialize the convertView and all its view's 
} 
    if(position %2==0) 
    { 
     view.setBackgroundColor(Color.BLUE); 
    }else{ 
     view.setBackgroundColor(Color.Red); 
    } 
    return view; 
} 
0
call from your activity : 
ListAdapteradapter = new ListAdapter(this, fillMaps, R.layout.listmain, from, to); 

ListAdapter class: 

public class ListAdapter extends SimpleAdapter { 

    public ListAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) 
    { 
     super(context, data, resource, from, to); 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 

    View view = super.getView(position, convertView, parent); 

     if (position % 2 == 1) 
     { 

      view.setBackgroundResource(R.color.blue); 


     } 
     else { 
      view.setBackgroundResource(R.color.lavendar); 
     } 
     return view; 
     } 



}