android - Create file of an ArrayList of java class -
i'm making android app reminds me when it's someone's birthday.
i'm able add name , corresponding date. but, i'm stuck @ saving part. i've tried looking solutions unable port needs. since it's arraylist , not simple string cannot serialized (if i'm correct?) , therefore not saved. there other way save kind of data?
here code:
item.java:
public class item implements serializable{ private string name; private string date; public item(string name, string date){ this.name = name; this.date = date; } public string getname(){ return name; } public string getdate(){ return date; } public void setname(string name){ this.name = name; } public void setdate(string date){ this.date = date; } }
mainactivity.java:
public class mainactivity extends appcompatactivity { recyclerview recyclerview; static recyclerviewadapter adapter; static arraylist<item> itemlist = new arraylist<>(); private static string nametext; private static string birthstring; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); recyclerview = (recyclerview) findviewbyid(r.id.recyclerview); linearlayoutmanager llm = new linearlayoutmanager(this); llm.setorientation(linearlayoutmanager.vertical); recyclerview.setlayoutmanager(llm); recyclerview.setitemanimator(new defaultitemanimator()); adapter = new recyclerviewadapter(itemlist); recyclerview.setadapter(adapter); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { addbirthday(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public static class datepickerfragment extends dialogfragment implements datepickerdialog.ondatesetlistener { @override public dialog oncreatedialog(bundle savedinstancestate) { // use current date default date in picker final calendar c = calendar.getinstance(); int year = c.get(calendar.year); int month = c.get(calendar.month); int day = c.get(calendar.day_of_month); // create new instance of datepickerdialog , return return new datepickerdialog(getactivity(), this, year, month, day); } public void ondateset(datepicker view, int year, int month, int day) { // date chosen user month++; birthstring = day + "-" + month + "-" + year; itemlist.add(new item(nametext, birthstring)); adapter.notifydatasetchanged(); savearraylist(getcontext(), itemlist); } } private void addbirthday() { alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("name"); final edittext input = new edittext(this); input.setinputtype(inputtype.type_class_text); builder.setview(input); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { nametext = input.gettext().tostring(); dialogfragment newfragment = new datepickerfragment(); newfragment.show(getsupportfragmentmanager(), "datepicker"); } }); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); builder.show(); } public static void savearraylist(context c, arraylist<item> arraylist) { } }
there's no problem serializing or deserializing arraylists in java:
list<item> list = new arraylist<>(); list.add(new item("a", "2006")); list.add(new item("b", "2016")); // serialize fileoutputstream fos = context.openfileoutput("list.ser", context.mode_private); objectoutputstream os = new objectoutputstream(fos); os.writeobject(list); os.close(); fos.close(); // deserialize fileinputstream filein = context.openfileinput("list.ser"); objectinputstream in = new objectinputstream(filein); list = (list<item>) in.readobject(); in.close(); filein.close(); // implement tostring() in item system.out.println(list);
Comments
Post a Comment