android - How to store this list view? -


i have 2 activites updating list view (one adding item , 1 removing item , displaying list) want store list(named schedule) after app closes , resume list when app reopens.

activity add items on list named schedule

listview.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() {         @override         public boolean onitemlongclick(final adapterview<?> parent, view view, final int position, long id) {              new alertdialog.builder(cloudevents.this)                     .seticon(android.r.drawable.ic_dialog_alert)                     .settitle("save event")                     .setmessage("do want save event schedule?")                     .setpositivebutton("yes", new dialoginterface.onclicklistener() {                         @override                         public void onclick(dialoginterface dialog, int which) {                              toast.maketext(cloudevents.this, "saved", toast.length_short).show();                              string itematposition = parent.getitematposition(position).tostring();                             intent intent = new intent(cloudevents.this, myschedule.class);                             myschedule.schedule.add(itematposition);                             startactivity(intent);                          }                     })                     .setnegativebutton("no", null)                     .show();              return true;         }     }); 

activity contains list view , has delete option upon long press

public class myschedule extends appcompatactivity {  static arraylist<string> schedule = new arraylist<>() ; listview schedulelistview; arrayadapter myarrayadapter; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_my_schedule);     settitle("schedule");     schedulelistview = (listview) findviewbyid(r.id.schedulelistview);     myarrayadapter = new arrayadapter(this, android.r.layout.simple_list_item_1, schedule);      schedulelistview.setadapter(myarrayadapter);     schedulelistview.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() {         @override         public boolean onitemlongclick(adapterview<?> parent, view view, final int position, long id) {              new alertdialog.builder(myschedule.this)                     .seticon(android.r.drawable.ic_dialog_alert)                     .settitle("are sure ?")                     .setmessage("do want delete note")                     .setpositivebutton("yes", new dialoginterface.onclicklistener() {                         @override                         public void onclick(dialoginterface dialog, int which) {                             schedule.remove(position);                             toast.maketext(myschedule.this,"deleted",toast.length_short).show();                             myarrayadapter.notifydatasetchanged();                         }                     })                     .setnegativebutton("no", null)                     .show();              return true;         }     }); } 

i tried using shared preferences in onpause , onresume method didnt worked , didnt update list beyond 2 items.i did this

  @override protected void onpause() {     super.onpause();      sharedpreferences = getapplicationcontext().getsharedpreferences("com.yatin.whatshappeningdtu", context.mode_private);     sharedpreferences.editor editor = sharedpreferences.edit();     gson gson = new gson();      string json = gson.tojson(schedule);      editor.putstring("key", json);     editor.commit(); }  @override protected void onresume() {     super.onresume();      sharedpreferences = getapplicationcontext().getsharedpreferences("com.yatin.whatshappeningdtu", context.mode_private);     gson gson = new gson();     string json = sharedpreferences.getstring("key", null);     type type = new typetoken<arraylist<string>>() {}.gettype();     schedule = gson.fromjson(json, type); } 

kimdly suggest method save list.thank !

while reload this.schedule adapter this.myarrayadapter still uses old content. try reload adapter

    @override     protected void onresume() {         super.onresume();          sharedpreferences = getapplicationcontext().getsharedpreferences("com.yatin.whatshappeningdtu", context.mode_private);         gson gson = new gson();         string json = sharedpreferences.getstring("key", null);         type type = new typetoken<arraylist<string>>() {}.gettype();         schedule = gson.fromjson(json, type);          myarrayadapter = new arrayadapter(this, android.r.layout.simple_list_item_1, schedule);         schedulelistview.setadapter(myarrayadapter);     } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -