Monday, 15 March 2010

Android showing database in SwipeView -



Android showing database in SwipeView -

i have swipeview 2 different views(fragments) via viewpager. want these 2 pages show different info sqllite database. problem cant figure out how this, main activity not able access views (tablelayout) on fragments , fragments sourcecode cant access database because adapter wont open (context of adapter super.getactivity()). there way this?

code of fragment:

dbadapter adapter = new dbadapter(getactivity()); @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub if(container==null){ homecoming null; } layout=(relativelayout)inflater.inflate(r.layout.layout_morning, container, false); adapter.open(); adapter.close();

code of dbadapter:

private static string db_name = "database.dat"; private final context context; private databasehelper dbhelper; private sqlitedatabase db; public dbadapter(context ctx) { this.context = ctx; dbhelper = new databasehelper(context); } private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, db_name, null, 1); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } } public dbadapter open() throws sqlexception { db = dbhelper.getwritabledatabase(); homecoming this; } public void close() { if (db!=null){ db.close(); } dbhelper.close(); }

error nullpointerexception

of course of study can access database within fragment, need utilize getactivity(); instead of this.

example save image file within fragment class:

db = new databasehandler(getactivity()); bytearrayoutputstream stream = new bytearrayoutputstream(); photo.compress(bitmap.compressformat.jpeg, 100, stream); byte[] imageinbyte = stream.tobytearray(); db.updateuser(imageinbyte); db.close();

android android-fragments android-viewpager android-sqlite android-context

No comments:

Post a Comment