0

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

Community
  • 1
  • 1
user1324936
  • 2,187
  • 4
  • 36
  • 49

1 Answers1

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

Community
  • 1
  • 1
Barak
  • 16,318
  • 9
  • 52
  • 84
  • Its working with for example textView's text. This means ViewBinder is only convering a few use cases. This makes it very weak – user1324936 May 27 '12 at 21:12
  • Not necessrily. it is working on everything **in the data source**. Your checkboxes are not part of the data source. If you put checkbox status into your data source (I did this for a shopping app a while ago), then it will read/set the state from that and you can create methods to toggle the checkbox in the data backing the adapter, which will then flow back to the list through the viewbinder. Most people don't want to do that though, so they add an array to hold the state of additional items they want to use and use `getView`instead. – Barak May 27 '12 at 21:18
  • what do you mean by data source and how do i put it into the data source? – user1324936 May 27 '12 at 21:35
  • Your data is coming from somewhere... An array or a cursor. That is your data source. How you add it is up to you and your implementation. I'm uisually using databases and cursors, so I add a field in the database to hold the checkstate, with methods to modify and save the state to the database in an `onClick` method. I pull it in my cursor and use it to set the checkbox state. – Barak May 27 '12 at 21:41
  • I gues i do that. I use a SimpleAdapter, which takes data from List> getData(String prefix) funktion, also the CheckBox information. Everything works ok until some elements become visible again after scrolling. – user1324936 May 27 '12 at 22:00
  • didnt help to handle data on my own. found out that the problem is my custom widget which gets destroyed and recreated or not destroyed but reused. – user1324936 May 27 '12 at 23:30
  • Problem was my custom Widget didnt redraw. It always got the correct data, but didnt show it. List reuses objects which showed their old values. – user1324936 May 28 '12 at 11:20