How to save an expandable listview in android? -
i have expandable listview can add too. how can save every item added. have tried using sharedpreferences gson json, doesn't seem work. here expandable listview adapter:
public class expandlistadapter extends baseexpandablelistadapter { private context context; private arraylist<group> groups; public expandlistadapter(context context, arraylist<group> groups) { this.context = context; this.groups = groups; } @override public object getchild(int groupposition, int childposition) { arraylist<child> chlist = groups.get(groupposition).getitems(); return chlist.get(childposition); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { child child = (child) getchild(groupposition, childposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.child_row, null); } textview tv = (textview) convertview.findviewbyid(r.id.country_name); tv.settext(child.getname().tostring()); return convertview; } @override public int getchildrencount(int groupposition) { arraylist<child> chlist = groups.get(groupposition) .getitems(); return chlist.size(); } @override public object getgroup(int groupposition) { return groups.get(groupposition); } @override public int getgroupcount() { return groups.size(); } @override public long getgroupid(int groupposition) { return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { group group = (group) getgroup(groupposition); if (convertview == null) { layoutinflater inf = (layoutinflater) context .getsystemservice(context.layout_inflater_service); convertview = inf.inflate(r.layout.group_header, null); } textview tv = (textview) convertview.findviewbyid(r.id.group_name); tv.settext(group.getname()); return convertview; } @override public boolean hasstableids() { return true; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; }
here class uses expandable listview, can add groups alertdialog , adds child name of "yo", testing reasons:
public class classesscreen extends activity implements adapterview.onitemselectedlistener { private expandlistadapter expadapter; private expandablelistview expandlist; private button newheaderbut; static arraylist<group> group_list; arraylist<child> child_list; string m_text; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_classes); group_list = new arraylist<group>(); expandlist = (expandablelistview) findviewbyid(r.id.classeslistview); expadapter = new expandlistadapter(this, group_list); expandlist.setadapter(expadapter); newheaderbut = (button) findviewbyid(r.id.newclassbut); newheaderbut.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { alertdialog.builder builder = new alertdialog.builder(classesscreen.this); builder.settitle("add new class:"); final edittext input = new edittext(classesscreen.this); builder.setview(input); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { m_text = input.gettext().tostring(); group gru1 = new group(); gru1.setname(m_text); child_list = new arraylist<child>(); child ch1 = new child(); ch1.setname("yo"); child_list.add(ch1); gru1.setitems(child_list); group_list.add(gru1); expadapter.notifydatasetchanged(); } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); builder.show(); }); } public void onitemselected(adapterview<?> parent, view view, int position, long id){} public void onnothingselected(adapterview<?> arg0) {} }
i have 2 object class called group , child. can show if need them. in advance!
save latest arraylist file
private void savedatatofile() { fileoutputstream fileoutputstream = null; try { fileoutputstream = getcontext().openfileoutput("filename", context.mode_private); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (nullpointerexception e) { } objectoutputstream objectoutputstream = null; try { objectoutputstream = new objectoutputstream(fileoutputstream); } catch (ioexception e) { e.printstacktrace(); } catch (nullpointerexception e) { } try { if (objectoutputstream != null) { objectoutputstream.writeobject(yourarraylist); //which data u want save } } catch (ioexception e) { e.printstacktrace(); } try { if (objectoutputstream != null) { objectoutputstream.close(); } } catch (ioexception e) { e.printstacktrace(); } }
retrieve data file
private void getdatafromfile() { fileinputstream fileinputstream = null; try { fileinputstream = getcontext().openfileinput("filename"); } catch (filenotfoundexception e) { e.printstacktrace(); return; } objectinputstream objectinputstream = null; try { objectinputstream = new objectinputstream(fileinputstream); } catch (ioexception |nullpointerexception e) { e.printstacktrace(); } try { yourarraylist = (arraylist<yourclassname>) objectinputstream.readobject(); } catch (ioexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } try { objectinputstream.close(); } catch (ioexception e) { e.printstacktrace(); } }
Comments
Post a Comment