java - I need a method to save an arraylist in my app -
im working on app , in made arraylist of strings. searched ways save list next visit did not find one. tried shared preferences didn't manage make work.
can please me find way save data in array list , restore in every app visit ?
the array want save called here "contactlist".
i put code below:
public class work_contacts extends activity { public static final int pick_contact = 1; private arraylist<string> contactlist; private arrayadapter<string> arrayadapter; private arraylist<string> contactlist2; private arrayadapter<string> arrayadapter2; @override public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.activity_work__contacts); button btnpickcontact = (button) findviewbyid(r.id.btnpickcontact); btnpickcontact.setonclicklistener(new view.onclicklistener() { public void onclick(view _view) { intent intent = new intent(intent.action_pick, contactscontract.commondatakinds.phone.content_uri); startactivityforresult(intent, pick_contact); } }); contactlist=new arraylist<string>(); contactlist2=new arraylist<string>(); arrayadapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, contactlist); arrayadapter2 = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, contactlist2); final listview lv = (listview) findviewbyid(r.id.contactlistview); lv.setadapter(arrayadapter); lv.setlongclickable(true); lv.setclickable(true); lv.setonitemlongclicklistener(new onitemlongclicklistener() { public boolean onitemlongclick(adapterview<?> arg0, view arg1, int pos, long id) { arrayadapter.notifydatasetchanged(); arrayadapter.remove(arrayadapter.getitem(pos)); log.v("long clicked","pos: " + pos); return true; } }); lv.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int pos, long id) { string url = "tel:"+arrayadapter2.getitem(pos); intent intent = new intent(intent.action_dial, uri.parse(url)); startactivity(intent); } }); } @suppresswarnings("deprecation") @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch (requestcode) { case (pick_contact): { if (resultcode == activity.result_ok) { uri contenturi = data.getdata(); //phone name cursor c = managedquery(contenturi, null, null, null, null); c.movetofirst(); string name = c.getstring(c.getcolumnindexorthrow(contactscontract.contacts.display_name)); //phone number string contactid = contenturi.getlastpathsegment(); cursor cursor = getcontentresolver().query(phone.content_uri, null, phone._id + "=?", new string[] { contactid }, null);// < - note, not contact_id! startmanagingcursor(cursor); boolean numbersexist = cursor.movetofirst(); int phonenumbercolumnindex = cursor .getcolumnindex(phone.number); string phonenumber = ""; while (numbersexist) { phonenumber = cursor.getstring(phonenumbercolumnindex); phonenumber = phonenumber.trim(); numbersexist = cursor.movetonext(); } stopmanagingcursor(cursor); //set arrayadapter.add(name + " - " + phonenumber); arrayadapter.notifydatasetchanged(); arrayadapter2.add(phonenumber); arrayadapter2.notifydatasetchanged(); } break; } } }
}
i store list in sqlite. can find many examples on web on how this.
Comments
Post a Comment