Android: Utilizing ListFragments for displaying pre made Database -
i'm beginner , origin understand android programming.
i'm utilizing slidingmenu libraries , i'm trying list items in fragment shown. (items displayed simply strings)
http://imageshack.us/photo/my-images/46/screenshot2013021419134.png/
however, when seek extract info pre made database , list items mentioned, i'm stuck never ending loading.
http://imageshack.us/photo/my-images/577/screenshot2013021923094.png/
here codes..
public class mainactivity extends slidingfragmentactivity { public static int theme = r.style.theme_sherlock; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // related slidingmenu setcontentview(r.layout.activity_main); setbehindcontentview(r.layout.primary_view); getfragmentmanager().begintransaction() .replace(r.id.primary_view, new fraglist()) .commit(); slidingmenu sm = getslidingmenu(); sm.setmode(slidingmenu.left_right); sm.settouchmodeabove(slidingmenu.touchmode_margin); sm.setshadowwidthres(r.dimen.shadow_width); sm.setshadowdrawable(r.drawable.shadow); sm.setbehindoffsetres(r.dimen.slidingmenu_offset); sm.setfadedegree(0.35f); sm.setsecondarymenu(r.layout.secondary_view); getfragmentmanager().begintransaction() .replace(r.id.secondary_view, new worshipfraglist()) .commit(); sm.setsecondaryshadowdrawable(r.drawable.shadowright); getsupportactionbar().setdisplayhomeasupenabled(true); setslidingactionbarenabled(false); } public boolean oncreateoptionsmenu(menu menu) { getsupportmenuinflater().inflate(r.menu.activity_main, menu); searchmanager searchmanager = (searchmanager) getsystemservice(context.search_service); searchview searchview = (searchview) menu.finditem(r.id.menu_search).getactionview(); if (null != searchview ) { searchview.setsearchableinfo(searchmanager.getsearchableinfo(getcomponentname())); searchview.seticonifiedbydefault(false); } homecoming super.oncreateoptionsmenu(menu); }
}
class songhelper extends sqliteopenhelper { private static string database_path = ""; private static final string database_name = "datadb"; public static final string table_name = "song"; public static final string column_id = "_id"; public static final string column_title = "title"; public sqlitedatabase dbsqlite; private final context mycontext; cursor cursor; public songhelper(context context) { super(context, database_name, null, 1); database_path = "/data/data/" + context.getpackagename() + "/databases/"; this.mycontext = context; } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } public void createdatabase() { createdb(); } private void createdb() { boolean dbexist = dbexists(); if(!dbexist) { this.getreadabledatabase(); copydbfromresource(); } } private boolean dbexists() { sqlitedatabase db = null; seek { string databasepath = database_path + database_name; db = sqlitedatabase.opendatabase(databasepath, null, sqlitedatabase.open_readwrite); db.setlocale(locale.getdefault()); db.setlockingenabled(true); db.setversion(1); } grab (sqliteexception e) { log.e("sqlhelper", "database not found"); } if (db!=null) { db.close(); } homecoming db!= null ? true : false; } private void copydbfromresource() { inputstream inputstream = null; outputstream outputstream = null; string dbfilepath = database_path + database_name; seek { inputstream = mycontext.getassets().open(database_name); outputstream = new fileoutputstream(dbfilepath); byte[] buffer = new byte[1024]; int length; while((length = inputstream.read(buffer)) > 0) { outputstream.write(buffer, 0, length); } outputstream.flush(); outputstream.close(); inputstream.close(); } catch(ioexception e) { throw new error("problem copying database resource file."); } } public void opendatabase() throws sqlexception { string mypath = database_path + database_name; dbsqlite = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); } @override public synchronized void close() { if (dbsqlite != null) { dbsqlite.close(); } super.close(); } public arraylist<string> gettablecolumn(string table_name, string[] column){ arraylist<string> al = new arraylist<string>(); cursor = dbsqlite.query(table_name, column, null, null, null, null, null); cursor.movetofirst(); while (!cursor.isafterlast()) { al.add(cursor.getstring(0)); cursor.movetonext(); } system.out.println(al); cursor.close(); homecoming al; } public string getname(cursor c) { return(c.getstring(1)); }
}
public class fraglist extends listfragment { songhelper dbsonghelper = null; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); dbsonghelper = new songhelper(getactivity()); }
}
help much appreciated find solution problem..
thanks :)
android database android-listfragment slidingmenu
No comments:
Post a Comment