Android - 可以在listview中更改按钮颜色

时间:2014-03-06 06:38:53

标签: android listview button colors

我可以使用simplecursoradapter更改listview中的按钮颜色 或者使用另一个可以用作显示类型的颜色条纹的对象以及如何将它用于此问题。

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

我的代码不起作任何人都可以帮助我......

1 个答案:

答案 0 :(得分:0)

您可以使用Button实例访问getView()中的convertview。之后,您可以访问和更改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);
}