ViewBinder creates random content

243 views Asked by At

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

There are 1 answers

7
Barak On BEST ANSWER

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