android - Unable to access query -
i db cannot resloved error in class. @ displaylistview() function. can please me solve it. thanks
got code here http://kdehairy.com/2012/08/19/using-a-preloaded-sqlite-database-with-sqliteopenhelper/
public class prepopsqlitedbactivity extends activity {
// private static final string db_name = "hymnals"; //a practice define database field names constants private simplecursoradapter dataadapter; private context context; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //our key helper externaldbopenhelper repo = externaldbopenhelper.getinstance( context ); sqlitedatabase db = repo.getwritabledatabase(); displaylistview(); } private void displaylistview() { //all hymns fetched *final cursor cursor = db.fetchallhymns();* // desired columns bound string[] columns = new string[] { externaldbopenhelper.hymn_id, externaldbopenhelper.hymn_name, }; // xml defined views data bound int[] = new int[] { r.id.tvid, r.id.name, }; // create adapter using cursor pointing desired data //as layout information dataadapter = new simplecursoradapter( this, r.layout.activity_main, cursor, columns, to, 0); listview listview = (listview) findviewbyid(r.id.list); // assign adapter listview listview.setadapter(dataadapter); listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> listview, view view, int position, long id) { // cursor, positioned corresponding row in result set cursor cursor = (cursor) listview.getitematposition(position); // state's capital row in database. } }); } }
the variable db
local variable within oncreate
method. use instance variable instead:
class /* ... */ { private sqlitedatabase db; /* ... */ @override public void oncreate(bundle savedinstancestate) { /* ... */ db = repo.getwritabledatabase(); /* ... */ } /* ... */ }
Comments
Post a Comment