Android Change the color of List view Item

六眼飞鱼酱① 提交于 2019-12-25 04:23:04

问题


   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;

                };
            };

Here I used the above code to color items according to the items included in addeditem(it is array).But my problem is that it changes the color of the other row views which were not newly added in my wArrayList.


回答1:


You have to do:

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


来源:https://stackoverflow.com/questions/21372920/android-change-the-color-of-list-view-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!