ViewBinder creates random content

情到浓时终转凉″ 提交于 2019-12-11 19:32:27

问题


I have strange behavoir in a ViewBinder. All works good on TextViews. With CheckBoxes (removed in my code) and custom widgets progressChart the values are not stored in the right postion of the List when I scroll down and/or up again. All seems very random. But TextViews are always correct. Here is my code:

SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object data,
                    String textRepresentation) {
                if (view.getId() == R.id.progress) {
                    ProgressChart progressChart = (ProgressChart) view;

                progressChart.setVisibility(ImageButton.GONE);
                    } else {
                        if (x.substring(0, 1).equals("1")) {
                            int theProgress = Integer.parseInt(x.substring(1));
                            progressChart.setProgressDarkRed(theProgress);
                        }
                }
                if (view.getId() == R.id.textView1) {
                    TextView textView = (TextView) view;
                    textView.setText((String) data);
                }
                return true;
            }
        };
        simpleAdapter.setViewBinder(viewBinder);
        setListAdapter(simpleAdapter);

Any help is highly appreciated

I found a similar problem but the answer doesnt fit to my code. Problems with the ViewBinder


回答1:


It is because of view recycling.

You need to create an object to hold the status of your checkboxes and set your views from that in your adapter.

I don't think it can be done in the viewbinder since the position is not passed in to it (but to be honest I've never tried)..

You will probably have to do it in getView.

Here is a link to a previous answer of mine where I show how to go about it with a SimpleCursorAdapter (it can be modified to appy to an arrayadapter too). SO Answer



来源:https://stackoverflow.com/questions/10776947/viewbinder-creates-random-content

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