Android更改列表视图项的颜色

时间:2014-01-27 04:14:23

标签: android android-listview

   final SimpleAdapter adapter = new SimpleAdapter(getActivity(),
                    itemlist, R.layout.listview_custom_view_sales_products,
                    new String[] { "Number", "item_name", "qty" }, new int[] {
                            R.id.number, R.id.Name, R.id.value }) {

                public View getView(int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);

                    HashMap<String, String> obj = (HashMap<String, String>) getListView().getAdapter().getItem(position);



                for (String x : added_items) {

                      if (String.valueOf(x).equals(obj.get("item_name").toString())) {

                            Log.i("sdsds", "working");

                            view.setBackgroundColor(Color.BLUE);

                        }

                    } 

                    return view;

                };
            };

在这里,我使用上面的代码根据 additem (它是数组)中包含的项目为项目着色。但我的问题是它改变了颜色我的wArrayList中没有新添加的其他行视图。

1 个答案:

答案 0 :(得分:2)

你必须这样做:

            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                String word=yourArrayOfStrings[position];
                if(word.equals("item_...")){
                    view.setBackgroundColor(Color.BLUE);

                }
                return view;
            }