android - Gathering SQLite row id to delete entry from context menu -
i query database , returns sorted arrays populate listview
. when click on listview
can obtain row id according database, context menu can't access same array, guidance on how go fixing this? of examples found online won't because database's row id not match listview
id because of sorting goes on.
this calls database , displays info:
public class sqlview extends listactivity { private simpleadapter adapter; listview drinklist; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); // full screen requestwindowfeature(window.feature_no_title); setcontentview(r.layout.sqlview); // definitions sharedpreferences somebar; somebar = getsharedpreferences("barpreferences", 0); string originalbar = somebar.getstring("currentbar", "my local bar"); originalbar = originalbar.replace("'", "''"); // open database , retrieve drinknames , abv values dbmanager info = new dbmanager(this); info.open(); customobject data = info.sortdata(originalbar); string[] drinknames = data.getnames(); string[] drinkscores = data.getscores(); final string[] drinkrowid = data.getrowid(); info.close(); drinklist = (listview) getlistview(); // create mapping string[] = new string[] { "rowid", "col_1" }; int[] = new int[] { r.id.item_tvdrinkname, r.id.item_tvabvvalue }; list<hashmap<string, string>> fillmaps = new arraylist<hashmap<string, string>>(); (int = 0; < drinknames.length; i++) { hashmap<string, string> map = new hashmap<string, string>(); map.put("rowid", "" + drinknames[i]); map.put("col_1", "" + drinkscores[i]); fillmaps.add(map); } adapter = new simpleadapter(this, fillmaps, r.layout.list_item, from, to); drinklist.setadapter(adapter); // register listview context menu registerforcontextmenu(getlistview()); drinklist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> a, view v, int position, long id) { toast.maketext(sqlview.this, "row id of database entry " + drinkrowid[(int) id], //this returns sqlite row id toast.length_short).show(); } }); } public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.context_menu, menu); } public boolean oncontextitemselected(menuitem item) { // ******this context menu in question adaptercontextmenuinfo information = (adaptercontextmenuinfo) item.getmenuinfo(); long index = information.id; switch (item.getitemid()) { case r.id.edit: toast.maketext(sqlview.this, "edit " + index, toast.length_short).show(); //this returns listview index break; case r.id.delete: toast.maketext(sqlview.this, "delete "+ index, toast.length_short).show(); break; } return super.onoptionsitemselected(item); }
}
this database helper class (sortdata()
method of interest, can see return arrays):
public class dbmanager { public static final string key_rowid = "_id"; public static final string key_name = "drink_name"; public static final string key_bar = "bar_name"; public static final string key_score = "drink_score"; public static final string key_vol = "volume"; public static final string key_price = "price"; public static final string key_drinkabv = "abv"; // strings abv public static final string key_abv_id = "_id"; public static final string key_name_abv = "drink_name"; public static final string key_abv = "abv_value"; private static final string database_name = "dbmanagerdb"; private static final string database_table = "drinktable"; private static final string abv_table = "abvtable"; private static final int database_version = 1; private dbhelper ourhelper; private final context ourcontext; private sqlitedatabase ourdatabase; private static class dbhelper extends sqliteopenhelper { public dbhelper(context context) { super(context, database_name, null, database_version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql("create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_bar + " text not null, " + key_name + " text not null, " + key_price + " double, "+ key_vol + " double, " + key_drinkabv + " double, " + key_score + " double);"); db.execsql("create table " + abv_table + " (" + key_abv_id + " integer primary key autoincrement, " + key_name_abv + " text not null, " + key_abv + " double);"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists " + database_table); db.execsql("drop table if exists " + abv_table); oncreate(db); } } public dbmanager(context c) { ourcontext = c; } public dbmanager open() throws sqlexception { ourhelper = new dbhelper(ourcontext); ourdatabase = ourhelper.getwritabledatabase(); return this; } public boolean deletedatabase(context context) { return context.deletedatabase(database_name); } public void close() { ourhelper.close(); } public long createentry(string bar, string name,double price, double vol, double abv, double score) { // todo auto-generated method stub contentvalues cv = new contentvalues(); cv.put(key_bar, bar); cv.put(key_name, name); cv.put(key_price, price); cv.put(key_vol, vol); cv.put(key_drinkabv, abv); cv.put(key_score, score); return ourdatabase.insert(database_table, null, cv); } //***************************this method of interest public customobject sortdata(string bar) { // todo auto-generated method stub string[] columns = new string[] { key_rowid, key_bar, key_name, key_score }; cursor c = ourdatabase.query(database_table, columns, key_bar + " ='" + bar + "'", null, null, null, key_score + " desc"); int iname = c.getcolumnindex(key_name); int iscore = c.getcolumnindex(key_score); int irow = c.getcolumnindex(key_rowid); c.movetolast(); string[] names = new string[c.getcount()]; double[] scores = new double[c.getcount()]; string[] scores1= new string[c.getcount()]; string[] rowid = new string[c.getcount()]; string result = ""; string myscores = ""; string rows=""; int count = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()) { rowid[count] = c.getstring(irow); scores[count] = c.getdouble(iscore); decimalformat df = new decimalformat("#.##"); scores1[count]= double.tostring(double.valueof(df.format(scores[count]))); /* in case break later (int =0; i<scores.length;i++){ scores1[i]= double.tostring(double.valueof(df.format(scores[i]))); system.out.println(scores1[i]); } */ //scores1[count] = double.tostring(c.getdouble(iscore)); names[count] = c.getstring(iname); //below string formatting //result = result + names[count] + "\n"; //myscores = myscores + string.format("%.2f", scores[count]) + "\n"; count++; } c.close(); //used format array it's pretty pretty return new customobject(names, scores1, rowid); } public class customobject { private string[] names; //array2 private string[] scores; //array1 private string[] rowid; public customobject(string[] names, string[] scores, string[] id){ this.names = names; this.scores = scores; this.rowid = id; } public string[] getnames() { return names; } public string[] getscores() { return scores; } public string[]getrowid() { return rowid; } //setters + getters. etc. } public string getname(long l) throws sqlexception { // todo auto-generated method stub string[] columns = new string[] { key_rowid, key_name, key_score }; cursor c = ourdatabase.query(database_table, columns, key_rowid + "=" + l, null, null, null, null); if (c != null) { c.movetofirst(); string name = c.getstring(1); return name; } return null; } public string getscore(long l) throws sqlexception { // todo auto-generated method stub string[] columns = new string[] { key_rowid, key_name, key_score }; cursor c = ourdatabase.query(database_table, columns, key_rowid + "=" + l, null, null, null, null); if (c != null) { c.movetofirst(); string score = c.getstring(2); return score; } return null; } public void updateentry(long lrow, string mname, string mscore) throws sqlexception { // todo auto-generated method stub contentvalues cvupdate = new contentvalues(); cvupdate.put(key_name, mname); cvupdate.put(key_score, mscore); ourdatabase.update(database_table, cvupdate, key_rowid + "=" + lrow, null); } public void deleteentry(long lrow1) throws sqlexception { // todo auto-generated method stub ourdatabase.delete(database_table, key_rowid + "=" + lrow1, null); } public boolean deleteeverything(context context) { // todo auto-generated method stub return context.deletedatabase(database_name); } public string[] viewabv() { // todo auto-generated method stub string[] columns = new string[] { key_abv_id, key_name_abv, key_abv}; cursor c = ourdatabase.query(abv_table, columns, null, null, null, null, null); int idrink = c.getcolumnindex(key_name_abv); int iabv = c.getcolumnindex(key_abv); int precounter = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()) { precounter++; } string[] drinks; double[] abv; int count = 0; // figure out how preallocate better drinks = new string[precounter]; abv = new double[precounter]; (c.movetofirst(); !c.isafterlast(); c.movetonext()) { abv[count] = c.getdouble(iabv); drinks[count] = c.getstring(idrink); count++; } c.close(); //real similar other ones, don't format long string. return drinks; } public long createentryabv(string name, string abv) { // todo auto-generated method stub contentvalues cv = new contentvalues(); double value = double.parsedouble(abv); cv.put(key_name_abv, name); cv.put(key_abv, value); return ourdatabase.insert(abv_table, null, cv); } public string getabv(string l) throws sqlexception { cursor c = ourdatabase.rawquery("select * abvtable trim(drink_name) = '"+l.trim()+"'", null); if (c != null) { c.movetofirst(); string abv = c.getstring(c.getcolumnindex("abv_value")); return abv; } return null; } }
you don't have id
in oncontextmenuitem()
callback have position
of row context menu built , can use find id:
public boolean oncontextitemselected(menuitem item) { adaptercontextmenuinfo information = (adaptercontextmenuinfo) item.getmenuinfo(); int rowposition = information.position; hashmap<string, string> rowdata = adapter.getitem(position); long index = rowdata.get("rowid"); // i'm assuming id // profit...
for non cursor based adapter id returned row position of row in adapter.
Comments
Post a Comment