android - closing sqlite in listview database with onclick method -


first of new developer of android learing please bear me. making app related book chapters , displaying content, got working listview populated database, facing bugs :-

  1. on leaving application having force close see log cat.

  2. in list view getting new data being appended in database. want 1 time only. chapter names constant book

  3. on chapter click, selected chapter list should open.

please let me know how correct them

mainactivity

public class mainactivity extends activity {      dbadchapter mydb;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          opendb();         registerlistclickcallback();      }      @override     protected void ondestroy() {         super.ondestroy();         closedb();         mydb.deleteall();     }      private void opendb() {         mydb = new dbadchapter(this);         mydb.open();         mydb.insertrow(999, "title 1");         mydb.insertrow(991, "title 2");         mydb.insertrow(992, "title 3");         mydb.insertrow(993, "title 4");         populatelistviewfromdb();     }      private void closedb() {         mydb.close();         mydb.deleteall();      }      public void onclick_addrecord(view v) {      }      private void populatelistviewfromdb() {         cursor cursor = mydb.getallrows();         startmanagingcursor(cursor);          string[] fromfieldnames = new string[] { dbadchapter.key_chepternum,                 dbadchapter.key_chapterheading };         int[] toviewids = new int[] { r.id.tvchapterno, r.id.tvchapter };         simplecursoradapter mycursoradapter = new simplecursoradapter(this,                 r.layout.item_view, cursor, fromfieldnames, toviewids);          listview mylist = (listview) findviewbyid(r.id.listchapter);         mylist.setadapter(mycursoradapter);      }      private void registerlistclickcallback() {          listview mylist = (listview) findviewbyid(r.id.listchapter);         mylist.setonitemclicklistener(new adapterview.onitemclicklistener() {              @override             public void onitemclick(adapterview<?> parent, view viewclicked,                     int position, long idindb) {                  cursor cursor = mydb.getrow(idindb);                 if (cursor.movetofirst()) {                  }                 cursor.close();              }         });     }      @override     protected void onpause() {          super.onpause();         closedb();         mydb.deleteall();     }  }  

database

public class dbadchapter2 {      private static final string tag = "dbadapter";      public static final string key_rowid = "_id";     public static final int col_rowid = 0;      public static final string key_chepternum = "chapternum";     public static final string key_chapterheading = "chapterheading";      public static final int col_chepternum = 1;     public static final int col_chapterheading = 2;      public static final string[] all_keys = new string[] { key_rowid,             key_chepternum, key_chapterheading };      public static final string database_name = "mydb";     public static final string database_table = "chapters";      public static final int database_version = 1;      private static final string database_create_sql = "create table "             + database_table + " (" + key_rowid             + " integer primary key autoincrement, "              + key_chepternum + " integer not null, " + key_chapterheading             + " string not null"              + ");";      private final context context;      private databasehelperchapter mydbhelper;     private sqlitedatabase db;      public dbadchapter2(context ctx) {         this.context = ctx;         mydbhelper = new databasehelperchapter(context);     }      public dbadchapter2 open() {         db = mydbhelper.getwritabledatabase();         return this;     }      public void close() {         mydbhelper.close();     }      public long insertrow(int number, string chapterheadings) {          contentvalues initialvalues = new contentvalues();         initialvalues.put(key_chepternum, number);         initialvalues.put(key_chapterheading, chapterheadings);          return db.insert(database_table, null, initialvalues);     }      public boolean deleterow(long rowid) {         string = key_rowid + "=" + rowid;         return db.delete(database_table, where, null) != 0;     }      public void deleteall() {         cursor c = getallrows();         long rowid = c.getcolumnindexorthrow(key_rowid);         if (c.movetofirst()) {             {                 deleterow(c.getlong((int) rowid));             } while (c.movetonext());         }         c.close();     }      public cursor getallrows() {         string = null;         cursor c = db.query(true, database_table, all_keys, where, null, null,                 null, null, null);         if (c != null) {             c.movetofirst();         }         return c;     }      public cursor getrow(long rowid) {         string = key_rowid + "=" + rowid;         cursor c = db.query(true, database_table, all_keys, where, null, null,                 null, null, null);         if (c != null) {             c.movetofirst();         }         return c;     }      private static class databasehelperchapter extends sqliteopenhelper {         databasehelperchapter(context context) {             super(context, database_name, null, database_version);         }          @override         public void oncreate(sqlitedatabase _db) {             _db.execsql(database_create_sql);         }          @override         public void onupgrade(sqlitedatabase _db, int oldversion, int newversion) {             log.w(tag, "upgrading application's database version "                     + oldversion + " " + newversion                     + ", destroy old data!");              _db.execsql("drop table if exists " + database_table);              oncreate(_db);         }     } } 

logcat

10-06 18:52:30.822: e/androidruntime(564): fatal exception: main 10-06 18:52:30.822: e/androidruntime(564): java.lang.runtimeexception: unable pause activity {com.bookz.bookz1/com.bookz.bookz1.mainactivity}: java.lang.illegalstateexception: database not open 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.activitythread.performpauseactivity(activitythread.java:2354) 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.activitythread.performpauseactivity(activitythread.java:2311) 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.activitythread.handlepauseactivity(activitythread.java:2291) 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.activitythread.access$1700(activitythread.java:117) 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.activitythread$h.handlemessage(activitythread.java:942) 10-06 18:52:30.822: e/androidruntime(564):  @ android.os.handler.dispatchmessage(handler.java:99) 10-06 18:52:30.822: e/androidruntime(564):  @ android.os.looper.loop(looper.java:123) 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.activitythread.main(activitythread.java:3683) 10-06 18:52:30.822: e/androidruntime(564):  @ java.lang.reflect.method.invokenative(native method) 10-06 18:52:30.822: e/androidruntime(564):  @ java.lang.reflect.method.invoke(method.java:507) 10-06 18:52:30.822: e/androidruntime(564):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 10-06 18:52:30.822: e/androidruntime(564):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 10-06 18:52:30.822: e/androidruntime(564):  @ dalvik.system.nativestart.main(native method) 10-06 18:52:30.822: e/androidruntime(564): caused by: java.lang.illegalstateexception: database not open 10-06 18:52:30.822: e/androidruntime(564):  @ android.database.sqlite.sqlitedatabase.querywithfactory(sqlitedatabase.java:1230) 10-06 18:52:30.822: e/androidruntime(564):  @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1189) 10-06 18:52:30.822: e/androidruntime(564):  @ com.bookz.bookz1.dbadchapter.getallrows(dbadchapter.java:130) 10-06 18:52:30.822: e/androidruntime(564):  @ com.bookz.bookz1.dbadchapter.deleteall(dbadchapter.java:117) 10-06 18:52:30.822: e/androidruntime(564):  @ com.bookz.bookz1.mainactivity.closedb(mainactivity.java:53) 10-06 18:52:30.822: e/androidruntime(564):  @ com.bookz.bookz1.mainactivity.onpause(mainactivity.java:79) 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.activity.performpause(activity.java:3851) 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.instrumentation.callactivityonpause(instrumentation.java:1191) 10-06 18:52:30.822: e/androidruntime(564):  @ android.app.activitythread.performpauseactivity(activitythread.java:2341) 10-06 18:52:30.822: e/androidruntime(564):  ... 12 more 

in onpause() method say:

     super.onpause();     closedb();     mydb.deleteall(); 

and closedb() following thing:

     mydb.close();     mydb.deleteall(); 

so 1 of errors in code call deleteall() twice no reason.

second, why call closedb , deleteall afterwards? simple logic says close database once you've finished work it. log cat says error "database not open" you've closed database trying more work it.

try switching places of deleteall , close, should fix problem.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -