android - ArrayList<String>() and SharedPreference using ObjectSerializer -
i have application saves array list of string , serializes using objectserializer saves serialized list in sharedpreferences instance.
it involves 2 activities "putting" , "getting" same sharepreferences object.
in listactivity, long-click item start deletion process. after string deletes, re-initialize listadapter updated arraylist<string> object update listview. after restart listacivity calling again, item reappears in list. causes nullpointerexception when clicked because points object doesnt exist anymore (null).
i have tried removing items arraylist<string> , putting in objectserializer sharedpreferences. tried removing old sharedpreference value (using clear() or remove() methods). maybe i'm overlooking step, cannot figure out can causing it.
would able show or walk me through how serialize arraylist<string>, save sharedpreference object, remove , item list, , update sharedpreference object can same (correctly updated list) sharedpreference object activity?
thank much!
below (shortened) code:
activity 1:
public static final string title_pref = "titlelist"; arraylist<string> titlelist = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); savebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { savesongfile(title, song); } }); } private void savesongfile(string title, string song) { bufferedwriter bufferwriter = null; try { fileoutputstream fos = openfileoutput(title, context.mode_private); bufferwriter = new bufferedwriter(new outputstreamwriter(fos)); bufferwriter.write(song); } catch (ioexception e) { e.printstacktrace(); } { try { bufferwriter.close(); } catch (ioexception e) { e.printstacktrace(); } } // new songs (not updated songs) go top if (!titlelist.contains(title)) titlelist.add(0, title); msettitlelistadapter(titlelist); savetitlearray(); } private void savetitlearray() { prefeditor = titlepref.edit(); try { prefeditor.putstring(title_pref, objectserializer.serialize(titlelist)); } catch (ioexception e) { e.printstacktrace(); } prefeditor.apply(); } private void msettitlelistadapter(arraylist<string> list) { autocompleteadapter = new arrayadapter<>( this, android.r.layout.simple_list_item_1, list ); titlebox.setadapter(autocompleteadapter); } activity 2 (listactivity):
public static final string extra_name = "com.frankbrenyah.lyricmaker.name"; public static final string title_pref = "titlelist"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_pick_song_list); //init string array blank list or sharedpref saved data songlistarray = new arraylist<>(); titlepref = getsharedpreferences(title_pref, mode_private); try { songlistarray = (arraylist<string>) objectserializer.deserialize( titlepref.getstring(title_pref, objectserializer.serialize(new arraylist<string>()) ) ); } catch (ioexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } msetlistadapter(songlistarray); getlistview().setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> parent, view view, final int position, long id) { final alertdialog.builder confirmdel = new alertdialog.builder(picksong_list.this); confirmdel.settitle("delete?") .setmessage("this cannot undone!") .setpositivebutton("do not delete", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { //system default dismiss() } }) .setnegativebutton("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { //delete song, update title list getapplicationcontext().deletefile(songlistarray.get(position)); songlistarray.remove(position); updatesharedpref(titlepref, title_pref, songlistarray); msetlistadapter(songlistarray); toast.maketext( getapplicationcontext(), "deleted", toast.length_short).show(); } }); confirmdel.create().show(); return true; } }); } @override protected void onlistitemclick (listview l, view v, int position, long id){ //return selected song main activity can loaded text box's intent resultintent = new intent(extra_name, uri.parse(songlistarray.get(position))); setresult(result_ok, resultintent); finish(); } private void msetlistadapter(arraylist<string> list) { final arrayadapter<string> arrayadapter = new arrayadapter<>( this, android.r.layout.simple_list_item_1, list ); setlistadapter(arrayadapter); } private void updatesharedpref(sharedpreferences sharedpref, string preffilename, arraylist<string> list) { sharedpreferences.editor editor = sharedpref.edit(); try { editor.putstring(preffilename, objectserializer.serialize(list)); } catch (ioexception e) { e.printstacktrace(); } editor.apply(); } addition: below 1 error instance randomly selected user.
java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=0, result=-1, data=intent { act=com.my.app.name dat=scrivo una canzone }} activity {com.my.app/com.my.app.activityone}: java.lang.nullpointerexception: attempt invoke virtual method 'void java.io.bufferedreader.close()' on null object reference @ android.app.activitythread.deliverresults(activitythread.java:3633) @ android.app.activitythread.handlesendresult(activitythread.java:3676) @ android.app.activitythread.access$1300(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1358) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5351) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) ... 10 more
Comments
Post a Comment