2014-03-06 49 views
1

我可以更改按鈕顏色列表視圖與simplecursoradapter 或使用其他對象,可以用它來是一個彩色條紋,顯示類型以及如何使用它的這個問題。Android的 - 可以改變按鈕顏色在列表視圖

public class ColorAdapter extends SimpleCursorAdapter { 
    private Context context; 

    public ColorAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     this.context = context; 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return MyArrList.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.event_list, null); 
     } 

     Button btype = (Button) findViewById(R.id.ctype); 

     if (MyArrList.get(position).get("EventType") 
       .equalsIgnoreCase("RED")) { 
      btype.setBackgroundColor(Color.RED); 
     } else if (MyArrList.get(position).get("EventType") 
       .equalsIgnoreCase("BLACK")) { 
      btype.setBackgroundColor(Color.BLACK); 
     } else { 
      btype.setBackgroundColor(Color.BLUE); 
     } 


     TextView tname = (TextView) convertView.findViewById(R.id.ename); 
     tname.setText(MyArrList.get(position).get("EventName")); 

     TextView tdetail = (TextView) convertView 
       .findViewById(R.id.edetail);// ãÊè¢éÍÁÙÅ·ÕÅÐÊèǹ 
     tdetail.setText(MyArrList.get(position).get("EventDetail")); 

     return convertView; 
    } 
} 

我的代碼不工作誰能幫我...

+0

它是一個錯誤/警告?或者只是代碼不起作用? – drulabs

+0

使用BaseAdapter而不是SimpleCursorAdapter。 – Shrikant

+0

'MyArrList'包含什麼? – GrIsHu

回答

0

您想使用convertview實例來訪問你的getView()Button。之後,您可以訪問和更改您的顏色Button

更改以下行

Button btype = (Button) findViewById(R.id.ctype); 

要如下:

Button btype = (Button) convertView.findViewById(R.id.ctype); 

而且從你的ArrayList退回商品的ID在你的下面的方法:

public Object getItem(int position) { 

    return MyArrList.get(position); 
} 
+0

謝謝我現在可以做到 – TooKom