Error while populate an ExpandableListView from SQLite Database in android -
Error while populate an ExpandableListView from SQLite Database in android -
am trying create expandablelistview populated info sqlite database , have 2 tables , , want create subjects_col column contents header of expandablelistview , , contents of other table kid of expandablelistview
the mainactivity class
public class mainactivity extends actionbaractivity { button badd; alertdialog.builder builder; expandablelistview exlv; database db; sqlitedatabase w; cursor cursor; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); badd = (button) findviewbyid(r.id.baddrule); exlv = (expandablelistview) findviewbyid(r.id.expandablelistview); badd.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { builder = new alertdialog.builder(mainactivity.this); builder.settitle("adding new rule"); view v = getlayoutinflater().inflate(r.layout.adding_new_rule_dialog_view , null); final edittext etsubjectname = (edittext) v.findviewbyid(r.id.etsubjectname); final edittext etruletitle = (edittext) v.findviewbyid(r.id.etruletitle); final edittext etruledesc = (edittext) v.findviewbyid(r.id.etruledesc); builder.setview(v); builder.setpositivebutton("ok" , new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { db = new database(builder.getcontext()); w = db.getwritabledatabase(); string subjectname = etsubjectname.gettext().tostring(); string ruletitle = etruletitle.gettext().tostring(); string ruledesc = etruledesc.gettext().tostring(); contentvalues cv1 = new contentvalues(); cv1.put(db.subjects_col , subjectname); w.insert(db.table_subjects , null , cv1 ); contentvalues cv2 = new contentvalues(); cv2.put(db.rules_title , ruletitle ); cv2.put(db.rules_desc , ruledesc); w.insert(db.table_rules , null , cv2); } }); builder.setnegativebutton("cancel" , null); builder.show(); } }); db = new database(this); w = db.getreadabledatabase(); cursor = w.query(db.table_subjects , null,null,null,null,null,null); cursor.movetofirst(); startmanagingcursor(cursor); adapter adapter = new adapter(this , cursor); exlv.setadapter(adapter); } class adapter extends simplecursortreeadapter{ public adapter(context context, cursor cursor) { super(context, cursor, r.layout.header_list_item, new string [] {database.subjects_col} , new int [] {r.id.tvsubject}, r.layout.child_list_item, new string [] {database.rules_title , database.rules_desc}, new int [] {r.id.tvtitle , r.id.tvdesc}); } @override protected cursor getchildrencursor(cursor gcursor) { cursor childcursor = db.fetchchildren(gcursor.getstring(gcursor.getcolumnindex(db.subjects_id))); mainactivity.this.startmanagingcursor(childcursor); childcursor.movetofirst(); homecoming childcursor; } } }
database class
public class database extends sqliteopenhelper { public static final string db_name = "notes"; public static final int db_version = 1; public static final string table_subjects = "subjects"; public static final string table_rules = "rules"; public static final string subjects_id = "id"; public static final string subjects_col = "subjects_col"; public static final string rules_id = "id"; public static final string rules_title = "rules_title" ; public static final string rules_desc = "rules_desc"; private sqlitedatabase db; public database(context context) { super(context, db_name, null, db_version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table if not exists " + table_subjects + "(" + subjects_id + " integer primary key autoincrement, " + subjects_col + " text );" ); db.execsql("create table if not exists " + table_rules +"(" +rules_id + " integer primary key autoincrement, " +rules_title + " text, " +rules_desc + " text, " +subjects_id +" reference" + table_subjects + ");" ); } @override public void onupgrade(sqlitedatabase db, int i, int i2) { db.execsql("drop table if exists " + table_subjects); db.execsql("drop table if exists " + table_rules); oncreate(db); } public cursor fetchgroup() { cursor c = db.query(table_subjects , new string [] {subjects_id ,subjects_col} ,null,null,null,null,null); log.d("tag" , c.getstring(c.getcolumnindex(subjects_col))); homecoming c; } public cursor fetchchildren(string id) { cursor c = db.query(table_rules , new string[] {rules_id , rules_title , rules_desc} , subjects_id , new string [] {id} , null,null,null ); homecoming c; } }
the dialog xml file
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textpersonname" android:ems="10" android:id="@+id/etsubjectname" android:hint="subject name" android:paddingtop="20dp" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textpersonname" android:ems="10" android:id="@+id/etruletitle" android:hint="rule title" android:paddingtop="20dp" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textmultiline" android:ems="10" android:id="@+id/etruledesc" android:layout_gravity="center_horizontal" android:hint="add rule here" android:paddingtop="20dp" /> </linearlayout>
header_list_item
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageview" android:src="@drawable/atom" android:layout_marginleft="2dp" android:layout_margintop="3dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="subject" android:id="@+id/tvsubject" android:layout_marginleft="10dp" android:layout_margintop="6dp" /> </linearlayout> kid xml file <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="1" android:id="@+id/tvrulenumber" android:layout_gravity="center_horizontal" android:paddingleft="10dp" android:paddingtop="5dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="title" android:id="@+id/tvtitle" android:paddingleft="10dp" android:paddingtop="5dp" /> </linearlayout> <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="desc" android:id="@+id/tvdesc" android:paddingleft="50dp" /> </linearlayout> </linearlayout>
and here exception
04-05 09:20:44.829 1975-1975/com.examples.anabil.mathphysicsrules e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.examples.anabil.mathphysicsrules/com.examples.anabil.mathphysicsrules.mainactivity}: java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow. create sure cursor initialized correctly before accessing info it. @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) @ android.app.activitythread.access$600(activitythread.java:141) @ android.app.activitythread$h.handlemessage(activitythread.java:1256) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5103) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) @ dalvik.system.nativestart.main(native method) caused by: java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow. create sure cursor initialized correctly before accessing info it. @ android.database.cursorwindow.nativegetlong(native method) @ android.database.cursorwindow.getlong(cursorwindow.java:507) @ android.database.abstractwindowedcursor.getlong(abstractwindowedcursor.java:75) @ android.widget.cursortreeadapter$mycursorhelper.getid(cursortreeadapter.java:436) @ android.widget.cursortreeadapter.getgroupid(cursortreeadapter.java:191) @ android.widget.expandablelistconnector.getitemid(expandablelistconnector.java:422) @ android.widget.adapterview.getitemidatposition(adapterview.java:768) @ android.widget.adapterview.setselectedpositionint(adapterview.java:1154) @ android.widget.listview.setadapter(listview.java:477) @ android.widget.expandablelistview.setadapter(expandablelistview.java:599) @ com.examples.anabil.mathphysicsrules.mainactivity.oncreate(mainactivity.java:86) @ android.app.activity.performcreate(activity.java:5133) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) at android.app.activitythread.handlelaunchactivity(activitythread.java:2261) at android.app.activitythread.access$600(activitythread.java:141) at android.app.activitythread$h.handlemessage(activitythread.java:1256) at android.os.handler.dispatchmessage(handler.java:99) at android.os.looper.loop(looper.java:137) at android.app.activitythread.main(activitythread.java:5103) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:525) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) at com.android.internal.os.zygoteinit.main(zygoteinit.java:553) at dalvik.system.nativestart.main(native method)
android sqlite expandablelistview
Comments
Post a Comment