Android Fragment Inheritance -
edit: in fixing problems pointed out @editpandaforce, stumbled upon problem. basefragment lines in logcat not fragment, ones leading one. oncreateview not called in fragment yet.
i having 2 problems android app i'm writing.
first, have fragmentfoo inheriting abstract basefragment. oncreateview() executes basefragment code. fragmentfoo.oncreateview() not execute @ all.
second, databasehandler db returning null in function fragmentfoo.loadstuff(), not null in basefragment.oncreateview(). oncreateview() called before loadstuff() is.
i'm guessing 2 related , i'm missing critical in inheritance relationship.
here code:
fragmentfoo:
public class fragmentfoo extends basefragment { private long flserial; @override protected void setupfragment() { stitle = "view foo"; idlayout = r.layout.fragment_foo; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.d("fragmentfoo", "oncreateview before"); view view = super.oncreateview(inflater, container, savedinstancestate); log.d("fragmentfoo", "oncreateview after"); try{ flsessionserial = this.getarguments().getlong("serial"); } catch (exception e) { flsessionserial = -1; } return view; } @override public void onattach(context context) { super.onattach(context); log.d("fragmentfoo", "onattach"); loadstuff(); } private void loadstuff() { log.d("fragmentfoo", "loadstuff - db=null:" + string.valueof(db == null)); cursor cursor = db.getcursor(flserial); log.d("fragmentfoo", "cursorloadloads - cursor=null: " + string.valueof(cursor == null)); string[] columns = new string[] {"foo", "bar"}; int[] ids = new int[] {r.id.textview_foo, r.id.textview_bar}; simplecursoradapter adapter = new simplecursoradapter(getactivity(), r.layout.adapter_foo, cursor, columns, ids, 0); listview lv = (listview) getactivity().findviewbyid(r.id.listview_foo); lv.setadapter(adapter); lv.setonitemclicklistener(this); } }
basefragment:
public abstract class basefragment extends fragment { protected string stitle; protected databasehandler db; protected int idlayout; protected abstract void setupfragment(); @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { setupfragment(); db = databasehandler.getinstance(getactivity()); log.d("basefragment", "oncreateview - db=null: " + string.valueof(db == null)); return inflater.inflate(idlayout, container, false); } }
databasehandler:
public class databasehandler extends sqliteopenhelper { private static databasehandler sinstance; private static context scontext; public static synchronized databasehandler getinstance(context context) { if (sinstance == null) { scontext = context; sinstance = new databasehandler(context.getapplicationcontext()); } return sinstance; } private databasehandler(context context) { super(context, database_name, null, database_version); } public cursor getcursor(){ sqlitedatabase db = getreadabledatabase(); string qry = "select * table"; cursor cursor = db.rawquery(qry, null); if (cursor != null) cursor.movetofirst(); return cursor; } }
the relevant logcat entries follows:
d/basefragment: oncreateview - db=null: false d/basefragment: oncreateview - db=null: false d/fragmentfoo: onattach d/fragmentfoo: loadstuff - db=null:true
Comments
Post a Comment