android - How to get items from a string-array using an ID-based mechanism? -
the problem simple. i've implemented navigation drawer in android. now, have menu, each item item of string-array
in strings.xml
. fragment created every time user clicks on 1 of menu items. within fragment class i'd switch
among items in order populate main content appropriate information. way know check position of item, happens if, in future, i'll add further item? need change position values in code.
so, wonder if there way assign sort of id each element. i've read here define separate string each of them, how can check values? tried in following way, gives me constant expression required
error:
resources resources = getresources(); switch(item_string) { case resources.getstring(r.string.item1): //todo case resources.getstring(r.string.item2): //todo ...
as suggested selvin, i've created json file in assets/
in each element of json array corresponds menu item including 3 fields (id, name , icon), e.g.:
[ {"id": 1, "name": "item1", "icon": "icon1"}, {"id": 2, "name": "item2", "icon": "icon2"}, {"id": 3, "name": "item3", "icon": "icon3"}, {"id": 4, "name": "item4", "icon": "icon4"} ]
then, firstly created item
class map json object:
public class item { private int id; private string name; private string icon; public item(jsonobject object) { try { this.id = object.getint("id"); this.name = object.getstring("name"); this.icon = object.getstring("icon"); } catch (jsonexception e) { e.printstacktrace(); } } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string geticon() { return icon; } public void seticon(string icon) { this.icon = icon; } // factory method convert array of json objects list of objects // user.fromjson(jsonarray); public static arraylist<item> fromjson(jsonarray jsonobjects) { arraylist<item> items = new arraylist<item>(); (int = 0; < jsonobjects.length(); i++) { try { items.add(new item(jsonobjects.getjsonobject(i))); } catch (jsonexception e) { e.printstacktrace(); } } return items; } }
and corresponding adapter:
public class itemadapter extends arrayadapter<item> { public itemadapter(context context, arraylist<item> items) { super(context, 0, items); } @override public view getview(int position, view convertview, viewgroup parent) { // data item position item item = getitem(position); int item_id = item.getid(); string item_name = item.getname(); int item_icon_id = parent.getcontext().getresources().getidentifier(item.geticon(), "drawable", parent.getcontext().getpackagename()); // check if existing view being reused, otherwise inflate view if (convertview == null) { convertview = layoutinflater.from(getcontext()).inflate(r.layout.drawer_list_item, parent, false); } // lookup view data population imageview item_icon_view = (imageview) convertview.findviewbyid(r.id.item_icon); textview item_name_view = (textview) convertview.findviewbyid(r.id.item_name); // populate data template view using data object item_name_view.settext(item_name); item_icon_view.setimageresource(item_icon_id); // return completed view render on screen return convertview; } }
finally in activity pass id of selected item:
fragment fragment = new mainfragment(); bundle args = new bundle(); args.putint(mainfragment.arg_item_id, item.getid()); args.putstring(mainfragment.arg_item_name, item.getname()); fragment.setarguments(args);
and switch in fragment in following:
int item_id = getarguments().getint(arg_item_id); switch(item_id) { case 1: [...] case 2: [...] [...]
hope example can other people.
Comments
Post a Comment