java - Saving checkbox state for dynamically created checkboxes in Android -
this question has answer here:
i have created listview consists of application installed on device. when installed applications displayed user, each application have corresponding checkbox. user clicks on multiple checkboxes , corresponding application names saved.
what want is, when user again starts application, checkboxes of applications saved user should checked.
how can ? can me? in advance.
here code
applist.java
public class applist extends activity { appinfoadapter adapter ; appinfo app_info[] ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_app_list); final listview listapplication = (listview)findviewbyid(r.id.listapplication); final button b= (button) findviewbyid(r.id.button001); final context context = null; b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub stringbuilder result = new stringbuilder(); for(int i=0;i<app_info.length;i++) { if(adapter.mcheckstates.get(i)==true) { result.append(app_info[i].applicationname).append(","); // result.append(","); } } string playlists = result.tostring(); storingclas.writeapps(applist.this, playlists); //string[] po = playlists.split(","); //b.settext(po[2]+"o"); } }); applicationinfo applicationinfo = getapplicationinfo(); packagemanager pm = getpackagemanager(); list<packageinfo> pinfo = new arraylist<packageinfo>(); pinfo.addall(pm.getinstalledpackages(0)); app_info = new appinfo[pinfo.size()]; int counter = 0; for(packageinfo item: pinfo){ try{ applicationinfo = pm.getapplicationinfo(item.packagename, 1); app_info[counter] = new appinfo(pm.getapplicationicon(applicationinfo), string.valueof(pm.getapplicationlabel(applicationinfo))); system.out.println(counter); } catch(exception e){ system.out.println(e.getmessage()); } counter++; } adapter = new appinfoadapter(this, r.layout.item_row, app_info); listapplication.setadapter(adapter); }}
appinfoadapter.java
public class appinfoadapter extends arrayadapter<appinfo> implements compoundbutton.oncheckedchangelistener { sparsebooleanarray mcheckstates; context context; int layoutresourceid; appinfo data[] = null; public appinfoadapter(context context, int layoutresourceid, appinfo[] data){ super(context, layoutresourceid,data); this.layoutresourceid = layoutresourceid; this.context = context; this.data = data; mcheckstates = new sparsebooleanarray(data.length); } @override public view getview(int position, view convertview, viewgroup parent){ view row = convertview; appinfoholder holder= null; if (row == null){ layoutinflater inflater = layoutinflater.from(context); row = inflater.inflate(layoutresourceid, parent, false); holder = new appinfoholder(); holder.imgicon = (imageview) row.findviewbyid(r.id.imageview1); holder.txttitle = (textview) row.findviewbyid(r.id.textview001); holder.chkselect = (checkbox) row.findviewbyid(r.id.checkbox1); row.settag(holder); } else{ holder = (appinfoholder)row.gettag(); } appinfo appinfo = data[position]; holder.txttitle.settext(appinfo.applicationname); holder.imgicon.setimagedrawable(appinfo.icon); // holder.chkselect.setchecked(true); // data[2].applicationname="kj"; /* string mm=storingclas.readapps(context); string[] po= mm.split(","); for(int k=0;k<po.length;k++){ for(int j=0;j<data.length;j++){ if(po[k].contains(data[j].applicationname)) { holder.chkselect.setchecked(mcheckstates.get(position, true)); } }}*/ holder.chkselect.settag(position); holder.chkselect.setchecked(mcheckstates.get(position, false)); holder.chkselect.setoncheckedchangelistener(this); return row; } public boolean ischecked(int position) { return mcheckstates.get(position, false); } public void setchecked(int position, boolean ischecked) { mcheckstates.put(position, ischecked); } public void toggle(int position) { setchecked(position, !ischecked(position)); } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { mcheckstates.put((integer) buttonview.gettag(), ischecked); } static class appinfoholder { imageview imgicon; textview txttitle; checkbox chkselect;}}
appinfo.java
public class appinfo { public drawable icon; public string applicationname; public appinfo(){ super(); } public appinfo(drawable icon, string applicationname){ super(); this.icon = icon; this.applicationname = applicationname; }}
there plenty of ways in android save information between app launches. android website provides great overview of different built-in ways: saving data
once choose way save, sharedpreference
or database, when app launched, can check saved information , display user.
Comments
Post a Comment