android - App Crashes On Startup Due To java.lang.IllegalArgumentException: column '_id' does not exist -
whenever start app, java.lang.illegalargumentexception: column '_id' not exist
error in logcat. have created column '_id'
, still throws this. here main .java:
package com.gantt.shoppinglist; import android.app.dialog; import android.app.listactivity; import android.database.cursor; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.simplecursoradapter; public class shoppinglist extends listactivity { private datahelper datahelper; /** called when activity first created. */ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); datahelper = new datahelper(this); cursor c = (cursor) datahelper.selectall(); long id = c.getlong(c.getcolumnindex("_id")); startmanagingcursor(c); listview lv = (listview) findviewbyid(android.r.id.list); string[] = new string[] { com.gantt.shoppinglist.datahelper.getdatabasename() }; int[] = new int[] { android.r.id.text1 }; simplecursoradapter adapter = new simplecursoradapter(this, android.r.layout.simple_list_item_1, c, from, to); lv.setadapter(adapter); button button1main = (button) findviewbyid(r.id.add); button1main.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { final dialog additem = new dialog(shoppinglist.this); additem.setcontentview(r.layout.maindialog); final edittext et = (edittext)additem.findviewbyid(r.id.edittext); additem.settitle("type item"); additem.setcancelable(true); et.sethint("type name of item..."); button button = (button) additem.findviewbyid(r.id.cancel); button.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { additem.dismiss(); } }); additem.show(); button ok = (button) additem.findviewbyid(r.id.ok); ok.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { final string text = et.gettext().tostring(); additem.dismiss(); et.settext(""); } }); } }); } }
here datahelper class:
package com.gantt.shoppinglist; import java.util.arraylist; import java.util.list; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.database.sqlite.sqlitestatement; import android.util.log; public class datahelper { private static final string database_name = "items.db"; private static final int database_version = 1; private static final string table_name = "table1"; public static final string key_rowid = "_id"; private context context; private sqlitedatabase db; private sqlitestatement insertstmt; private static final string insert = "insert " + table_name + "(name) values (?)"; public datahelper(context context) { this.context = context; openhelper openhelper = new openhelper(this.context); this.db = openhelper.getwritabledatabase(); this.insertstmt = this.db.compilestatement(insert); } public long insert(string name) { this.insertstmt.bindstring(1, name); return this.insertstmt.executeinsert(); } public void deleteall() { this.db.delete(table_name, null, null); } public cursor selectall() { list<string> list = new arraylist<string>(); cursor cursor = this.db.query(table_name, new string[] { "name" }, null, null, null, null, "name desc"); if (cursor.movetofirst()) { { list.add(cursor.getstring(0)); } while (cursor.movetonext()); } if (cursor != null && !cursor.isclosed()) { cursor.close(); } return cursor; } public static string getdatabasename() { return database_name; } private static class openhelper extends sqliteopenhelper { openhelper(context context) { super(context, getdatabasename(), null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + "(id integer primary key, name text"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w("example", "upgrading database, drop tables , recreate."); db.execsql("drop table if exists " + table_name); oncreate(db); } } }
i had similar issue - think it's case of having 'select' (or 'select as') called _id
because simplecursoradapter
needs it.
from documentation:
handling content uri ids
by convention, providers offer access single row in table accepting content uri id value row @ end of uri. convention, providers match id value table's
_id
column, , perform requested access against row matches.this convention facilitates common design pattern apps accessing provider. app query against provider , displays resulting
cursor
inlistview
usingcursoradapter
. definition ofcursoradapter
requires 1 of columns incursor
_id
.
in case, have autonumber column in table called 'oid' altered select command (for example)...
select oid _id, name, number mytable
this cured problem me.
edit show more extensive code...
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.channel_selector); gridview channel_selector_grid = (gridview) findviewbyid(r.id.channel_grid); sca = getguideadapter(); channel_selector_grid.setadapter(sca); } public simplecursoradapter getguideadapter() { simplecursoradapter adapter = null; sqlitedatabase db = sqlitedatabasehelper.getreadabledatabase(); cursor cursor = db.rawquery("select distinct oid _id, name, number chan_table order number", null); if (cursor.movetofirst()) { string[] columnnames = { "name" }; int[] resids = { r.id.channel_name }; adapter = new simplecursoradapter(this, r.layout.channel_selector_item, cursor, columnnames, resids); } return adapter; }
Comments
Post a Comment