listview - Android: how to save ArrayList items even if app shutdown? -
i developing app in have show installed applications in listview checkboxes.whenever user selects checkbox list application info object (corresponding checked checkbox) added global class variable (arraylist added_apps). succeed in doing so. problem whenever exits app of items user added added_apps arraylist not saved. have again add items in arraylist proceed further. have tried far , here code not work.
code displaying installed application , added global class variable added_apps arraylist on checkboxclick listener:
mainactivity.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context="com.example.pc.fkidshell.teen3activity"> <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:layout_marginbottom="20dp" android:text="add selected apps" android:width="170dp" android:layout_alignparentbottom="true" android:layout_gravity="center_horizontal" /> </linearlayout>
row.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="match_parent"> <imageview android:id="@+id/app_icon" android:layout_width="50dp" android:layout_height="50dp" android:padding="3dp" android:scaletype="centercrop" /> <linearlayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center_vertical" android:orientation="vertical" android:paddingleft="5dp" > <textview android:id="@+id/app_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:textstyle="bold" /> <textview android:id="@+id/app_paackage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" /> </linearlayout> <checkbox android:id="@+id/cb_app" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
activitymain.java:
public class teen3activity extends listactivity { private packagemanager packagemanager = null; private list<applicationinfo> applist = null; private applicationadapter listadaptor = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_teen3); //teenadd=(toolbar) findviewbyid(r.id.teenaddtb); //setsupportactionbar(teenadd); //getsupportactionbar().settitle("restrict app's"); button btn1=(button) findviewbyid(r.id.btn_add); btn1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { myaddedapss.writelist(teen3activity.this,myaddedapss.getadded_apps()); //here storing arraylist of added applications file if(myaddedapss.getadded_apps().isempty()) { toast.maketext(getapplicationcontext(), "nothing add!", toast.length_long).show(); } else { toast.maketext(getapplicationcontext(), "apps added!", toast.length_long).show(); } } }); packagemanager = getpackagemanager(); new loadapplications().execute(); } @override protected void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); applicationinfo app = applist.get(position); try { intent intent = packagemanager.getlaunchintentforpackage(app.packagename); if (null != intent) { startactivity(intent); } } catch (activitynotfoundexception e) { toast.maketext(teen3activity.this, e.getmessage(), toast.length_long).show(); } catch (exception e) { toast.maketext(teen3activity.this, e.getmessage(), toast.length_long).show(); } } private list<applicationinfo> checkforlaunchintent(list<applicationinfo> list) { arraylist<applicationinfo> applist = new arraylist<applicationinfo>(); (applicationinfo info : list) { try { if (null != packagemanager.getlaunchintentforpackage(info.packagename)) { applist.add(info); } } catch (exception e) { e.printstacktrace(); } } return applist; } private class loadapplications extends asynctask<void, void, void> { private progressdialog progress = null; @override protected void doinbackground(void... params) { applist = checkforlaunchintent(packagemanager.getinstalledapplications(packagemanager.get_meta_data)); listadaptor = new applicationadapter(teen3activity.this, r.layout.row, applist); return null; } @override protected void oncancelled() { super.oncancelled(); } @override protected void onpostexecute(void result) { setlistadapter(listadaptor); progress.dismiss(); super.onpostexecute(result); } @override protected void onpreexecute() { progress = progressdialog.show(teen3activity.this, null, "loading application info..."); super.onpreexecute(); } @override protected void onprogressupdate(void... values) { super.onprogressupdate(values); } } }
application adapter:
public class applicationadapter extends arrayadapter<applicationinfo> { private list<applicationinfo> appslist = null; private context context; private packagemanager packagemanager; private arraylist<boolean> checklist = new arraylist<boolean>(); public applicationadapter(context context, int textviewresourceid, list<applicationinfo> appslist) { super(context, textviewresourceid, appslist); this.context = context; this.appslist = appslist; packagemanager = context.getpackagemanager(); (int = 0; < appslist.size(); i++) { checklist.add(false); } } @override public int getcount() { return ((null != appslist) ? appslist.size() : 0); } @override public applicationinfo getitem(int position) { return ((null != appslist) ? appslist.get(position) : null); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; if (null == view) { layoutinflater layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view = layoutinflater.inflate(r.layout.row, null); } applicationinfo data = appslist.get(position); if (null != data) { textview appname = (textview) view.findviewbyid(r.id.app_name); textview packagename = (textview) view.findviewbyid(r.id.app_paackage); imageview iconview = (imageview) view.findviewbyid(r.id.app_icon); checkbox checkbox = (checkbox) view.findviewbyid(r.id.cb_app); checkbox.settag(integer.valueof(position)); // set tag can identify correct row in listener checkbox.setchecked(checklist.get(position));// set status stored checkbox.setoncheckedchangelistener(mlistener); // set listener appname.settext(data.loadlabel(packagemanager)); packagename.settext(data.packagename); iconview.setimagedrawable(data.loadicon(packagemanager)); } return view; } compoundbutton.oncheckedchangelistener mlistener = new compoundbutton.oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { checklist.set((integer)buttonview.gettag(),ischecked); // tag know row , store status if(ischecked) { int addapppos = (integer) buttonview.gettag(); applicationinfo p=appslist.get(addapppos); myaddedapss.setadded_apps(p); buttonview.setenabled(false); } } }; }
myaddedapps.java: (global class contain write method save arraylist file)
public class myaddedapss extends application { private static arraylist<applicationinfo> added_apps=new arraylist<applicationinfo>(); public static void setadded_apps(applicationinfo element) { added_apps.add(element); } public static arraylist<applicationinfo> getadded_apps(){ return added_apps; } public static boolean remove_app(applicationinfo element) { return added_apps.remove(element); } public static arraylist<applicationinfo> readlist(context c) { try { fileinputstream fis = c.openfileinput("name"); objectinputstream = new objectinputstream(fis); arraylist<applicationinfo> list = (arraylist<applicationinfo>)is.readobject(); is.close(); return list; } catch(exception ex) { ex.printstacktrace(); return null; } } public static void writelist(context c, arraylist<applicationinfo> list){ try { fileoutputstream fos = c.openfileoutput("name", context.mode_private); objectoutputstream os = new objectoutputstream(fos); os.writeobject(list); os.close(); } catch(exception ex) { ex.printstacktrace(); } } }
first, need convert list of objects json string. after can save string in shared preferences. when app wake up, can retrieve list shared preferences.
Comments
Post a Comment