android - How to use prepopulated database in listview -
i'm trying put app uses prepopulated sqlite database when loaded, populate list view. i'm new android , have been using guides online me through. using examples site: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
i unsure how set start populating data list view. pointers or examples helpful.
public class databasehelper extends sqliteopenhelper{ //the android's default system path of application database. private static string db_path = "/data/data/com.example.testsqllite/databases/"; private static string db_name = "testdatabase"; private sqlitedatabase mydatabase; private final context mycontext; public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } public void createdatabase() throws ioexception{ boolean dbexist = checkdatabase(); if(dbexist){ //do nothing - database exist }else{ //by calling method , empty database created default system path //of application gonna able overwrite database our database. this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database"); } } } /** * check if database exist avoid re-copying file each time open application. * @return true if exists, false if doesn't */ private boolean checkdatabase(){ sqlitedatabase checkdb = null; try{ string mypath = db_path + db_name; checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); }catch(sqliteexception e){ //database does't exist yet. } if(checkdb != null){ checkdb.close(); } return checkdb != null ? true : false; } /** * copies database local assets-folder created empty database in * system folder, can accessed , handled. * done transfering bytestream. * */ private void copydatabase() throws ioexception{ //open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = db_path + db_name; //open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); //transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer))>0){ myoutput.write(buffer, 0, length); } //close streams myoutput.flush(); myoutput.close(); myinput.close(); } public void opendatabase() throws sqlexception{ //open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); } @override public synchronized void close() { if(mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } // add public helper methods access , content database. // return cursors doing "return mydatabase.query(....)" it'd easy // create adapters views.
}
i think excellent tutorial on creating , using database class. have register, don't have verify don't have use real info.
Comments
Post a Comment