android - Passing Extras to listView ArrayAdapter -
i have 2 buttons, when click them, launches new listview activity. before starts activity, make bundle, putstring depending on button clicked , putextras.
in listview activity, have 2 string arrays. want arrayadapter getextras , read variable know button pressed there , i'll know string array load arrayadapter.
here idea arrayadapter doesn't accept string, though s = name of 1 of string arrays. also, i'm not sure if i'm approaching in best way.
bundle b = this.getintent().getextras(); string s = b.getstring("text"); setlistadapter(new arrayadapter<string>(this, r.layout.list_item, s));
the array adapter takes list of objects third parameter documentation on public constructors suggests (source developer android):
constructor arrayadapter(context context, int textviewresourceid, t[] objects) constructor arrayadapter(context context, int resource, int textviewresourceid, t[] objects) constructor arrayadapter(context context, int textviewresourceid, list<t> objects) constructor arrayadapter(context context, int resource, int textviewresourceid, list<t> objects)
so need encapsulate string in array (even if have 1 string display).
arraylist<string> arr = new arraylist<string>(); arr.add(s); setlistadapter(new arrayadapter<string>(this, r.layout.list_item, arr));
edit ok. why don't try basic if-else statement? like:
arraylist<string> arrbutton1 = new arraylist<string>(); arraylist<string> arrbutton2 = new arraylist<string>(); arraylist<string> arrtochoose; if(s.equals("button1")) { arrtochoose = arrbutton1; } else if(s.equals("button2")) { arrtochoose = arrbutton2; } setlistadapter(new arrayadapter<string>(this, r.layout.list_item, arraytochoose));
Comments
Post a Comment