java - How to save a SparseBooleanArray using SharedPreferences? Or any other alternative? -
java - How to save a SparseBooleanArray using SharedPreferences? Or any other alternative? -
i'd save sparsebooleanarray using either sharedpreferences, or else, that's more preferred.
i've tried using http://stackoverflow.com/a/16711258/2530836, everytime activity created, bundle re-initialized, , bundle.getparcelable() returns null. i'm sure there workaround this, haven't arrived @ despite few hours of brainstorming. 
also, if there isn't, utilize sharedpreferences?
here's code:
public class contactactivity extends activity implements adapterview.onitemclicklistener {      list<string> name1 = new arraylist<string>();     list<string> phno1 = new arraylist<string>();     arraylist<string> exceptions = new arraylist<string>();     myadapter adapter;     button select;     bundle bundle = new bundle();     context contactactivitycontext;     sharedpreferences prefs;     sharedpreferences.editor editor;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.contacts_layout);         getallcontacts(this.getcontentresolver());         listview list= (listview) findviewbyid(r.id.lv);         adapter = new myadapter();         list.setadapter(adapter);         list.setonitemclicklistener(this);         list.setitemscanfocus(false);         list.settextfilterenabled(true);         prefs = preferencemanager.getdefaultsharedpreferences(this);         select = (button) findviewbyid(r.id.button1);         select.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 stringbuilder checkedcontacts = new stringbuilder();                 (int = 0; < name1.size(); i++)                 {                     if (adapter.ischecked(i)) {                         checkedcontacts.append(name1.get(i).tostring());                         exceptions.add(name1.get(i).tostring());                         checkedcontacts.append(", ");                     }                 }                 checkedcontacts.deletecharat(checkedcontacts.length() - 2);                 checkedcontacts.append("selected");                 editor = prefs.edit();                 editor.putint("status_size", exceptions.size()); /* skey array */                  for(int i=0;i<exceptions.size();i++)                 {                     editor.remove("status_" + i);                     editor.putstring("status_" + i, exceptions.get(i));                 }                 editor.commit();                 toast.maketext(contactactivity.this, checkedcontacts, toast.length_long).show();             }         });     }       @override     public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) {         // todo auto-generated method stub         adapter.toggle(arg2);     }     private void setcontext(context contactactivitycontext){         this.contactactivitycontext = contactactivitycontext;     }     protected context getcontext(){          homecoming contactactivitycontext;     }      public  void getallcontacts(contentresolver cr) {          cursor phones = cr.query(contactscontract.commondatakinds.phone.content_uri, null,null,null, contactscontract.commondatakinds.phone.display_name + " asc" );         while (phones.movetonext())         {             string name=phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.display_name));             string phonenumber = phones.getstring(phones.getcolumnindex(contactscontract.commondatakinds.phone.number));             name1.add(name);             phno1.add(phonenumber);         }         phones.close();     }     class myadapter extends baseadapter implements compoundbutton.oncheckedchangelistener     {   sparsebooleanarray mcheckstates;         contactactivity mcontactactivity = new contactactivity();         layoutinflater minflater;         textview tv1,tv;         checkbox cb;         int mpos;         myadapter()         {             mcheckstates = new sparsebooleanarray(name1.size());             mcheckstates = (sparsebooleanarray) bundle.getparcelable("mybooleanarray");             minflater = (layoutinflater) contactactivity.this.getsystemservice(context.layout_inflater_service);         }         @override         public int getcount() {             // todo auto-generated method stub              homecoming name1.size();         }          public void setposition(int p){             mpos = p;         }          public int getposition(){              homecoming mpos;         }           @override         public object getitem(int position) {             // todo auto-generated method stub              homecoming position;         }          @override         public long getitemid(int position) {             // todo auto-generated method stub               homecoming 0;         }          @override         public view getview(final int position, view convertview, viewgroup parent) {             // todo auto-generated method stub             view vi=convertview;             if(convertview==null)             vi = minflater.inflate(r.layout.row, null);             tv= (textview) vi.findviewbyid(r.id.textview1);             tv1= (textview) vi.findviewbyid(r.id.textview2);             cb = (checkbox) vi.findviewbyid(r.id.checkbox1);             tv.settext("name :"+ name1.get(position));             tv1.settext("phone no :"+ phno1.get(position));             cb.settag(position);             cb.setchecked(mcheckstates.get(position, false));             setposition(position);             cb.setoncheckedchangelistener(this);               homecoming vi;         }         public boolean ischecked(int position) {              homecoming 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);         }     }     @override     public void ondestroy(){         bundle.putparcelable("mybooleanarray", new sparsebooleanarrayparcelable(adapter.mcheckstates));         super.ondestroy();     } }       
you can store in sharedpreferances thing sparsebooleanarrays is: maps integers boolean values, hence can save string, eliminate braces,spaces , = signs, , have int value (for index value) , corresponding boolean value, separated comma, utilize string accordingly. hope helps :)
 java android sharedpreferences 
 
  
Comments
Post a Comment