0

I have a listview containing checkbox and textview and I've added a ViewBinder to my adapter and in this viewbinder I've added an onchecked state listener to my checkboxes and in the onchecked state listener I wrote this code:

OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String id=buttonView.getTag()+"";
                int buttonid=Integer.valueOf(id);               
                if(buttonView.isChecked()==true){   
                    Log.d("checked","checked");
                myDbHelper.MarkAsFavorite(buttonid);

                }

                else if(buttonView.isChecked()==false){
                    Log.d("unchecked","unchecked");
                    myDbHelper.UnMarkAsFavorite(buttonid);
                }

                 cu=myDbHelper.GetCursor();
                 adapter.swapCursor(cu);

            }
        }; 

but with this code when my check box was getting checked it was saved in the database then when I scroll through the listview it gets unchecked all byitself it accesses the code of the unchecked and unchecks the checkbox and save that in the database.

So I fixed it with adding an onclick listener to the checkbox instead of the onchange state listener:

public void onclick(View view) {
         String id=view.getTag()+"";
            int buttonid=Integer.valueOf(id);               
            if(((CompoundButton) view).isChecked()==true){  
                Log.d("checked","checked");
            myDbHelper.MarkAsFavorite(buttonid);

            }

            else if(((CompoundButton) view).isChecked()==false){
                Log.d("unchecked","unchecked");
                myDbHelper.UnMarkAsFavorite(buttonid);
            }

             cu=myDbHelper.GetCursor();
             adapter.swapCursor(cu);
     }

And I have no more problem. But I wanted to know why this change state listener acted that way?

halfer
  • 19,824
  • 17
  • 99
  • 186
Learning Android
  • 273
  • 4
  • 17

2 Answers2

1

You have this checkbox inside ListView as ListView reuses the views for efficiency same checkbox object may provided to multiple ListView items and when it is checked for same check box is unchecked for another.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • yeah so the listview reuses the checkbox and it calls its changed listener because the other one(the other checkbox where the first one is placed and reused) is not checked so it unchecks the first one to place it in the place of the unchecked one and calls the listener... got it thanks. – Learning Android Jan 09 '14 at 17:14
  • but when rethinking, I've added to each checkbox a certain tag with the number of each one and the database will be changed depending on this number... how will it change the check state of the first checkbox while the tag is no more the one of the first one but the one of the second checkbox where the first one is reused..? – Learning Android Jan 09 '14 at 17:28
  • hard to follow this can you explain with more detail? Maybe update your que a bit with the scenario. – vipul mittal Jan 09 '14 at 17:42
  • if you can see my code I've written: String id=buttonView.getTag()+""; this is taking the tag of the button that was changed because in the viewbinder I tagged each checkbox with an id so in the changelistener I am only calling the button with its id which is the id of the changed button so how will it access this code when its id is different from the first button because the checkbox is resude but with another id... – Learning Android Jan 09 '14 at 17:48
  • All the views that are visible are different and in getView where you are setting the tag your buttonView has the tag that is visible – vipul mittal Jan 09 '14 at 17:55
  • I didn't really understand that but if you can give me some link where I can get this info from I would appreciate that :) – Learning Android Jan 09 '14 at 18:09
  • http://stackoverflow.com/questions/3817628/recycling-views-in-a-listview-worth-it – vipul mittal Jan 09 '14 at 18:13
  • 1
    search for ListView reusing views you will find more links. – vipul mittal Jan 09 '14 at 18:14
  • ok thank u very much because I don't really know what to search for... thanks will search for that.. – Learning Android Jan 09 '14 at 18:18
1

This change state listener acted that ways then only when you scroll a listview because its getview call again which changes the checked check box state therefore checkchangedlistner call which changes value in database. And when you used onclick your problem resolved because on you changes listview view onclick event not fires which is not changing your database value.

You can resolve this problem by using viewholder in listview.

Gaurav Berry
  • 131
  • 9