java - How to use the master detail flow theme with dynamic data? -
by creating new android project , selecting master detail flow theme, provide sample on how use theme, good. issue example uses static data , need dynamic. application uses async task data service in json format pars , save in phone db.
this dummycontent class:
public class dummycontent { /** * array of sample (dummy) items. */ public static list<dummyitem> items = new arraylist<dummyitem>(); /** * map of sample (dummy) items, id. */ public static map<string, dummyitem> item_map = new hashmap<string, dummyitem>(); // static { // // add 3 sample items. // additem(new dummyitem("1", "item 1")); // additem(new dummyitem("2", "item 2")); // additem(new dummyitem("3", "item 3")); // } public static void setcontext(context c) { if (db == null) db = new mydbadapter(c); // sqliteopenhelper + sqlitedatabase manager if (db.isopen() == false) { db.open(); cursor c = db.getprofiles(); // database query if (c.movetofirst()) { { profileitem item = new profileitem(c.getstring(0), c.getstring(1), c.getstring(2)); additem(item); } while (c.movetonext()); } } } private static void additem(dummyitem item) { items.add(item); item_map.put(item.id, item); } /** * dummy item representing piece of content. */ public static class dummyitem { public string id; public string content; public dummyitem(string id, string content) { this.id = id; this.content = content; } @override public string tostring() { return content; } } }
you can see commented static data , added "setcontext" should called add data db. issue , when should call function data show up? should call in "kind_name_listfragment.java" "oncreate" or "kind_name_detailfragment.java" "oncreate" or both (if both wont redundant) or somewhere else , there better solution?
my issue example uses static data , need dynamic
most straightforward way call dummycontent.setcontext(context) before accessing dummycontent.items:
(kind_name_listfragment.java) @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // call initialize dummycontent.items dummycontent.setcontext(getactivity()); setlistadapter(new arrayadapter<dummycontent.dummyitem>(getactivity(), android.r.layout.simple_list_item_activated_1, android.r.id.text1, dummycontent.items)); } you need call dummycontent.setcontext(getactivity()) whenever need initialize/update/refresh items , item_map. perhaps, placing call items.clear() , item_map.clear() @ beginning of setcontext(context) when refreshing data.
should call in "kind_name_listfragment.java" "oncreate" or "kind_name_detailfragment.java" "oncreate" or both (if both wont redundant) or somewhere else
call once above in kind_name_listfragment.oncreate(bundle), before setlistadapter(...). no need call again in kind_name_detailfragment.oncreate(bundle). when need update/refresh data:
dummycontent.setcontext(getactivity()); ((youradapter)getlistadapter()).notifydatasetchanged();
Comments
Post a Comment