1

Can I change button color in listview with simplecursoradapter or use another object that can use to be a color stripe that show type and how I can use it for this problem.

    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;
    }
}

My Code is not Work Can anyone help me...

TooKom
  • 35
  • 6

1 Answers1

0

You are suppose to access your Button in getView() by using convertview instance. After that you can access and change your Button color.

Change the below line

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

To as below:

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

Also return your item id from your ArrayList in your below method:

public Object getItem(int position) {

    return MyArrList.get(position);
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102