【问题标题】:Android - Can change button color in listviewAndroid - 可以在列表视图中更改按钮颜色
【发布时间】:2014-04-08 15:30:26
【问题描述】:

我可以用 simplecursoradapter 更改列表视图中的按钮颜色吗 或者使用另一个可以用作颜色条纹的对象来显示类型以及我如何使用它来解决这个问题。

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

我的代码无法正常工作任何人都可以帮助我...

【问题讨论】:

  • 这是错误/警告吗?还是只是代码不起作用?
  • 使用 BaseAdapter 而不是 SimpleCursorAdapter。
  • MyArrList 包含什么?

标签: android listview button colors


【解决方案1】:

您应该使用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);
}

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    相关资源
    最近更新 更多