android - Add new string to resource string array dynamically -
im making workout log , want ther user able add new exercise types.
i made string array in resources , spinner take strings string array in resources.
im not sure how add new string resources string array java code. tried code, exception.
thanks helping :)
public void onclick(view v) { switch(v.getid()){ case r.id.setnewexercise: string[] exercisestringarray = getresources().getstringarray(r.array.exercisetypes); arrayadapter<charsequence> list = arrayadapter.createfromresource(this, r.array.exercisetypes, android.r.layout.simple_list_item_1); string exercisecheck = addnewexercise.gettext().tostring(); try{ (int = 0; < exercisestringarray.length; i++) { if (exercisecheck.equals(exercisestringarray[i])) { toast.maketext(getapplicationcontext(), "this exercise exist", toast.length_long).show(); break; } else if (i == exercisestringarray.length - 1 && exercisecheck.equals(exercisestringarray[i]) == false) { list.add(exercisecheck); toast.maketext(getapplicationcontext(), "added successfully", toast.length_long).show(); break; } } }catch(exception e){ toast.maketext(getapplicationcontext(), "wtf", toast.length_long).show(); } } }
ive tried code, , spinner loaded empty , force closed when trying save new string.
/////////////////////exercise spinner///////////////////// // create arrayadapter using string array , default spinner layout list = new arraylist<charsequence>(); exerciseadapter = new arrayadapter<charsequence>(this, r.array.exercisetypes, list); // specify layout use when list of choices appears exerciseadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // apply adapter spinner workoutchoose.setadapter(exerciseadapter); } @override public void onclick(view v) { switch(v.getid()){ case r.id.setnewexercise: string exercisecheck = addnewexercise.gettext().tostring(); try{ if (exerciseadapter.getposition(exercisecheck) >= 0) { toast.maketext(getapplicationcontext(), "this exercise exist", toast.length_long).show(); break; } else { exerciseadapter.add(exercisecheck); exerciseadapter.notifydatasetchanged(); toast.maketext(getapplicationcontext(), "added successfully", toast.length_long).show(); break; } } catch(exception e){ toast.maketext(getapplicationcontext(), "wtf", toast.length_long).show(); } } }
you cannot modify resources themselves. welcome convert string[]
arraylist<string>
, add in additional strings, obtained elsewhere (e.g., file, database, sharedpreferences
) if wish.
add()
on arrayadapter
works if arrayadapter
made arraylist
, not java array ([]
), java array's size cannot changed @ runtime.
Comments
Post a Comment