I created a ListView with a CursorAdapter, populated by a table of my db. After I added a ContextMenu that with a long click on the desired line of ListView pick info from my table.
The problem is that: I can not collect the information of that single line of ListView, each line gives me the same result. I think I do something wrong in query.
onCreate:
private void createTabRegistry(SQLiteDatabase db)
{
String sql = "CREATE TABLE {0} ({1} INTEGER PRIMARY KEY AUTOINCREMENT, {2} TEXT,{3} INTEGER,{4} TEXT,{5} TEXT,{6} TEXT, {7} TEXT);";
db.execSQL(MessageFormat.format(sql, TabRegistry.TABLE_NAME, TabRegistry._ID,TabRegistry.TYPE, TabRegistry.DATE, TabRegistry.STATUS, TabRegistry.NUMBER, TabRegistry.MESSAGE, TabRegistry.OTHER));
}
My query in db class:
public Cursor getRegistry2(long id) {
return (getReadableDatabase().query(
TabRegistry.TABLE_NAME,
TabRegistry.COLUMNS,
TabRegistry._ID + " = " + id,
null,
null,
null,
null,
null));
}
id of ContextMenu in my activity:
AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
id = getListAdapter().getItemId(info.position);
My cursor in the activity class:
Cursor c3 = databaseHelper.getRegistry2(id);
if (c3 != null) {
c3.moveToFirst();
}
dbnumber = (c3.getString(c3.getColumnIndex(TabRegistry.NUMBER)));
How do I assign to dbnumber string the value of the line of ListView that wonder?
EDIT code that works, but reset the view of listview….
Cursor c3 = (Cursor) getListAdapter().getItem(info.position);
startManagingCursor(c3);
aaaaa = c3.getString(c3.getColumnIndex(TabRegistry.NUMBER));
c3.close();
AdapterView.AdapterContextMenuInfohas a field calledidthat represents the row id from the database of that selected row. You’ll want to query for that id and not thepositionfield that represents the position in the list:and then query your database for the
rowIdto get that item’s data.Edit:
You’ll probably don’t want to use the solution you added. You close the cursor so no more data(that is why you have to restart the activity, as you probably query the database only in the
onCreatemethod).I told you the check if
info.idreturns different values when you use long press(to show theContextMenu) different rows in the list, that means that theContextMenu(probably) selects different row ids for different rows).I don’t know what causes your issue(if the delete works I would say there is something in the activity), so I made a small example of what you are trying to do so you can see this type of code:
http://pastebin.com/Yri03S0T