java - How to properly use load array and save array methods? -
i have following code designed user click on button , when click on button, particular string viewing favorited , stored somewhere else.
i have 2 questions.
one what's wrong have right now? crashes when click button.
how go finishing on load array method string loaded , saved array user see array later?
thanks time!!
fav.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { loadarray("favorites"); favorites = arrays.copyof(favorites, favorites.length+1); favorites[favorites.length]=display.gettext().tostring(); savearray(favorites, "favorites"); } }); home.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent openstartingpoint = new intent("starting.rt.menu2"); startactivity(openstartingpoint); } }); search.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent openstartingpoint = new intent("starting.rt.search"); startactivity(openstartingpoint); } }); moreapps.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent gotomarket; gotomarket = new intent(intent.action_view, uri .parse("market://search?q=pub:\"wompa\"")); startactivity(gotomarket); } }); } public string[] loadarray(string arrayname) { sharedpreferences prefs = getsharedpreferences("preferencename", 0); int size = prefs.getint(arrayname + "_size", 0); string array[] = new string[size]; for(int i=0;i<size;i++) array[i] = prefs.getstring(arrayname + "_" + i, null); return array; } public boolean savearray(string[] array, string arrayname) { sharedpreferences prefs = getsharedpreferences("preferencename", 0); sharedpreferences.editor editor = prefs.edit(); editor.putint(arrayname +"_size", array.length); for(int i=0;i<array.length;i++) editor.putstring(arrayname + "_" + i, array[i]); return editor.commit(); }
log cat
05-08 23:10:51.329: i/process(495): sending signal. pid: 495 sig: 9 05-08 23:11:02.360: d/dalvikvm(504): gc_external_alloc freed 44k, 53% free 2553k/5379k, external 1625k/2137k, paused 221ms 05-08 23:11:08.599: d/dalvikvm(504): gc_external_alloc freed 16k, 51% free 2644k/5379k, external 2707k/3308k, paused 174ms 05-08 23:11:12.570: d/androidruntime(504): shutting down vm 05-08 23:11:12.570: w/dalvikvm(504): threadid=1: thread exiting uncaught exception (group=0x40015560) 05-08 23:11:12.599: e/androidruntime(504): fatal exception: main 05-08 23:11:12.599: e/androidruntime(504): java.lang.nullpointerexception 05-08 23:11:12.599: e/androidruntime(504): @ starting.rt.base$1.onclick(base.java:52) 05-08 23:11:12.599: e/androidruntime(504): @ android.view.view.performclick(view.java:2485) 05-08 23:11:12.599: e/androidruntime(504): @ android.view.view$performclick.run(view.java:9080) 05-08 23:11:12.599: e/androidruntime(504): @ android.os.handler.handlecallback(handler.java:587) 05-08 23:11:12.599: e/androidruntime(504): @ android.os.handler.dispatchmessage(handler.java:92) 05-08 23:11:12.599: e/androidruntime(504): @ android.os.looper.loop(looper.java:123) 05-08 23:11:12.599: e/androidruntime(504): @ android.app.activitythread.main(activitythread.java:3683) 05-08 23:11:12.599: e/androidruntime(504): @ java.lang.reflect.method.invokenative(native method) 05-08 23:11:12.599: e/androidruntime(504): @ java.lang.reflect.method.invoke(method.java:507) 05-08 23:11:12.599: e/androidruntime(504): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 05-08 23:11:12.599: e/androidruntime(504): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 05-08 23:11:12.599: e/androidruntime(504): @ dalvik.system.nativestart.main(native method) 05-08 23:11:17.869: i/process(504): sending signal. pid: 504 sig: 9
loadarray
function returns string[]
. might want use like:
string [] arr = loadarray("favorites");
this array can used later asked in 2nd part. hope helps.
update [based on comment]
so, if want grab text in textview , wish add in list/string array:
1) try string textview
textview display = (textview)findviewbyid(r.id.display); string tosave = display.gettext().tostring();
2) existing list sharedpreferences using `loadarray':
string [] arr = loadarray("favorites");
3) add new string it:
arraylist<string> arrlist = new arraylist<string>(); (int i=0; i<arr.length(); i++) { arrlist.add(arr[i]); //add old values arraylist } // add new string it: arrlist.add(tosave); //get array string [] array = arrlist.toarray(new string[arrlist.size()]);
4) save again.
savearray(array, "favorites");
Comments
Post a Comment