android - saving checkbox states from list with shared preferences -


i asked question on how save checkbox state using textfile recommended use shared preferences. had tried method before using textfiles had same issue - checkboxes in list check , uncheck seemingly randomly.

my app displays list of installed apps on device, displays title of app, icon , checkbox. if check 1 of items , scroll down , again, uncheck , other items checked randomly. i'm trying checkbox states loaded shared preferences, , saved when user manually checks box.

here code listview custom adapter:

    public class appdetailsadapter extends baseadapter {  private list<appdetails> data; private context context;   public appdetailsadapter(context context, list<appdetails> data) {     this.context = context;     this.data = data; }   @override public view getview(int position, view convertview, viewgroup parent) {     view view = convertview;      if (view == null) {         layoutinflater vi = (layoutinflater) context.getsystemservice(context.layout_inflater_service);         view = vi.inflate(r.layout.custom_row_layout, null);     }      imageview icon = (imageview) view.findviewbyid(r.id.icon);     textview text = (textview) view.findviewbyid(r.id.text);     final checkbox checkbox = (checkbox) view.findviewbyid(r.id.checkbox);     final appdetails item = data.get(position);      text.settext(item.name);     icon.setimagedrawable(item.icon);      sharedpreferences settings = context.getsharedpreferences("data",context.mode_private);     boolean checked = settings.getboolean(item.name, false);     checkbox.setchecked(checked);      checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {      @override     public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {             // handle conditions here         if(checkbox.ischecked()==true){              sharedpreferences settings = context.getsharedpreferences("data", context.mode_private);             settings.edit().putboolean(item.name,true).commit();             toast.maketext(context,"you selected "+item.name, toast.length_short).show();         }         else {             sharedpreferences settings = context.getsharedpreferences("data", context.mode_private);             settings.edit().putboolean(item.name,false).commit();             toast.maketext(context,"you deselected "+item.name, toast.length_short).show();         }         }     });       return view; } 

}

this happening because of view recycling.

can u try 1 thing:
instead of

   if (view == null) {         layoutinflater vi = (layoutinflater) context.getsystemservice(context.layout_inflater_service);         view = vi.inflate(r.layout.custom_row_layout, null);     } 

just write

  layoutinflater vi = (layoutinflater) context.getsystemservice(context.layout_inflater_service);     view = vi.inflate(r.layout.custom_row_layout, null); 

run again.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -