android - How to get _ID with SimpleCursorAdapter -
so far got this
db = new mydbhelper(this); constantscursor = db.getreadabledatabase().rawquery( "select _id, title, value " + "from constants order title", null); listadapter adapter = new simplecursoradapter(this, r.layout.item, constantscursor, new string[] { dbhelpermen2.title, dbhelpermen2.value }, new int[] {r.id.item_name, r.id.item_points }); listata = (listview) findviewbyid(r.id.listview1); listata.setadapter(adapter);
but have 1 more field named item_number. field want write _id number
how can ?
new simplecursoradapter(this, r.layout.item, constantscursor, new string[] { **???** ,mydbhelper.title, mydbhelper.value }, new int[] {**r.id.item_number** , r.id.item_name, r.id.item_points });
here mydbhelper
public class mydbhelper extends sqliteopenhelper { private static final string database_name = "db"; static final string title = "title"; static final string value = "value"; public mydbhelper(context context) { super(context, database_name, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table constants (_id integer primary key autoincrement, title text, value text);"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists constants"); oncreate(db); } }
simply this:
listadapter adapter = new simplecursoradapter(this, r.layout.item, constantscursor, new string[] { dbhelpermen2.id, dbhelpermen2.title, dbhelpermen2.value }, new int[] {r.id.item_number, r.id.item_name, r.id.item_points });
where in dbhelpermen2.id, id
name of column in db
this code should work:
public class mydbhelper extends sqliteopenhelper { private static final string database_name = "db"; static final string table_name = "constants"; static final string id = "_id"; static final string title = "title"; static final string value = "value"; public mydbhelper(context context) { super(context, database_name, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table "+ table_name + "(" + id + " integer primary key autoincrement, " + title + " text, " + value + " text);"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); }
}
Comments
Post a Comment