Monday 15 February 2010

android - Custom Cursor Adapter Error When Running, Illegal Argument Exception -



android - Custom Cursor Adapter Error When Running, Illegal Argument Exception -

i have updated post changes, inserted '_id', , working should

i hope can help problem quite new android , java. have created app uses sqlite database, using custom cursor adapter print out 1 line of database along 2 buttons , edittext. think have right code when seek , run app getting illegalargumentexception, have been looking through forum few days , still stuck. if point out error , help me prepare great!

this main activity

import android.app.activity; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.os.handler; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.button; import android.widget.listview; import android.widget.toast; import com.pinchtapzoom.r; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; public class myactivity extends activity { private customcursoradapter customadapter; //private com.example.rory.dbtest.dbadapter databasehelper; public listview list1; com.example.rory.dbtest.dbadapter db = new com.example.rory.dbtest.dbadapter(this); //customcursoradapter c = new customcursoradapter(this,c); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my); list1 = (listview)findviewbyid(r.id.data_list); db.open(); button addbtn = (button)findviewbyid(r.id.add); addbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(myactivity.this, addassignment.class); startactivity(i); } }); button deletebtn = (button)findviewbyid(r.id.delete); deletebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(myactivity.this, delete.class); startactivity(i); } }); button updatebtn = (button)findviewbyid(r.id.update); updatebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(myactivity.this, update.class); startactivity(i); } }); seek { string destpath = "/data/data/" + getpackagename() + "/databases/assignmentdb"; file f = new file(destpath); if (!f.exists()) { copydb( getbasecontext().getassets().open("mydb"), new fileoutputstream(destpath)); } } grab (filenotfoundexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } new handler().post(new runnable() { @override public void run() { //log.d("test", "customadapter " + customadapter.tostring()); //log.d("test", "databasehelper " + databasehelper.tostring()); customadapter = new customcursoradapter(myactivity.this, db.getallrecords()); list1.setadapter(customadapter); } }); } private class dbadapter extends baseadapter { private layoutinflater minflater; //private arraylist<> @override public int getcount() { homecoming 0; } @override public object getitem(int arg0) { homecoming null; } @override public long getitemid(int arg0) { homecoming 0; } @override public view getview(int arg0, view arg1, viewgroup arg2) { homecoming null; } } public void copydb(inputstream inputstream, outputstream outputstream) throws ioexception { //---copy 1k bytes @ time--- byte[] buffer = new byte[1024]; int length; while ((length = inputstream.read(buffer)) > 0) { outputstream.write(buffer, 0, length); } inputstream.close(); outputstream.close(); } }

this database class

import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class dbadapter { public static final string key_rowid = "_id"; public static final string key_item = "item"; public static final string key_litres = "litres"; private static final string tag = "dbadapter"; private static final string database_name = "dripdrop"; private static final string database_table = "assignments"; private static final int database_version = 1; private static final string database_create = "create table if not exists assignments (_id integer primary key autoincrement, " + "item varchar not null, litres date );"; private final context context; private databasehelper dbhelper; private sqlitedatabase db; public dbadapter(context ctx) { this.context = ctx; dbhelper = new databasehelper(context); } private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { seek { db.execsql(database_create); } grab (sqlexception e) { e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists contacts"); oncreate(db); } } //---opens database--- public dbadapter open() throws sqlexception { db = dbhelper.getwritabledatabase(); homecoming this; } //---closes database--- public void close() { dbhelper.close(); } //---insert record database--- public long insertrecord(string item, string litres) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_item, item); initialvalues.put(key_litres, litres); homecoming db.insert(database_table, null, initialvalues); } //---deletes particular record--- public boolean deletecontact(long rowid) { homecoming db.delete(database_table, key_rowid + "=" + rowid, null) > 0; } //---retrieves records--- public cursor getallrecords() { homecoming db.query(database_table, new string[] {key_rowid, key_item, key_litres}, null, null,null, null, null); } //---retrieves particular record--- public cursor getrecord(long rowid) throws sqlexception { cursor mcursor = db.query(true, database_table, new string[] {key_rowid, key_item, key_litres}, key_rowid + "=" + rowid, null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } homecoming mcursor; } //---updates record--- public boolean updaterecord(string item, string litres) { db.execsql("update "+database_table+" set "+key_litres+"='"+litres+"' "+key_item+"='"+item+"'"); homecoming true; } }

this custom cursor class import android.content.context; import android.database.cursor; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.cursoradapter; import android.widget.textview; import com.pinchtapzoom.r;

public class customcursoradapter extends cursoradapter { public customcursoradapter(context context, cursor c) { super(context, c); } @override public view newview(context context, cursor cursor, viewgroup parent) { // when view created first time, // need tell adapters, how each item layoutinflater inflater = layoutinflater.from(parent.getcontext()); view retview = inflater.inflate(r.layout.row, parent, false); homecoming retview; } @override public void bindview(view view, context context, cursor cursor) { // here setting our info // means, take info cursor , set in views textview textviewpersonname = (textview) view.findviewbyid(r.id.item1); textviewpersonname.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(1)))); } }

and logcat error getting when run app

4501-4501/com.example.rory.dbtest e/androidruntime﹕ fatal exception: main process: com.example.rory.dbtest, pid: 4501 java.lang.illegalargumentexception: column '_id' not exist @ android.database.abstractcursor.getcolumnindexorthrow(abstractcursor.java:303) @ android.widget.cursoradapter.init(cursoradapter.java:172) @ android.widget.cursoradapter.<init>(cursoradapter.java:120) @ com.example.rory.dbtest.customcursoradapter.<init>(customcursoradapter.java:19) @ com.example.rory.dbtest.myactivity$4.run(myactivity.java:94)

basically need alter id column _id because when using custom cursor adapter requires column _id there. it's unwritten rule of using databases custom cursor adapter

android sqlite logcat

No comments:

Post a Comment