Change color of an item in ListView with SimpleAdapter

我的未来我决定 提交于 2019-11-28 14:11:48
Gene

There is a method inside SimpleAdapter for that. It's called ViewBinder. Try to include this line of code immediately after: SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_cibo, from, to);” and before “setListAdapter(adapter);.

SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object object, String value) {
System.out.println("view= "+view);
    System.out.println("view.toString()= "+ view.toString());
    System.out.println("view.getId()= "+ view.getId());
    System.out.println("view.getVisibility()= "+ view.getVisibility());
    System.out.println("view.equals((TextView) view.findViewById(R.id. ricettaTipo))= "+ view.equals((TextView) view.findViewById(R.id. ricettaTipo)));
    if (view.equals((TextView) view.findViewById(R.id.ricettaTipo)))
            {
                TextView ricetta = (TextView) view.findViewById(R.id.ricettaTipo);
                    //Change color/answer/etc for ricettaTipo
            }

//OR
          if (view instanceof TextView) {
                    //Do stuff
                    return true;
                }

                return false;
            }
            };
adapter.setViewBinder(binder);

setListAdapter(adapter);    

setViewValue method will be called for each R.id.ricettaTipo, R.id.ricettaTitolo, R.id.ricettaDifficolta, R.id.ricettaTempo, R.id.ricettaIngredienti, R.id.ricettaVino, R.id.ricettaConsigli, R.id.ricettaPrep, R.id.ricettaPersone. The setViewValue method will be called each View/each time one of the above R.id’s is being drawn.

Why don't you try extending the SimpleAdapter class with a custom adapter class, maybe recipe adapter, and overwrite the getView(int position, View convertView, ViewGroup parent) Method.

        public View getView(int position, View convertView, ViewGroup parent) {
            return createViewFromResource(position, convertView, parent, mResource);
        }

        private View createViewFromResource(int position, View convertView,
                ViewGroup parent, int resource) {
            View v;
            if (convertView == null) {
                v = mInflater.inflate(resource, parent, false);
                /* set a background color depending on the recipe shown in this table 
                 * row you will have to include a recipe.getType() method 
                 * to determine the necessary color
                 * v.setBackgroundColor(R.color.your_favourite_color)
                 */        

                final int[] to = mTo;
                final int count = to.length;
                final View[] holder = new View[count];

                for (int i = 0; i < count; i++) {
                    holder[i] = v.findViewById(to[i]);
                }

                v.setTag(holder);
            } else {
                v = convertView;
            }

            bindView(position, v);

            return v;
        }

the code is taken from grepcode

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