I have a XML layout which has two edit text fields, one for “title” and the the other for “story”,when the user enters his data in these text fields, and presses the back button , the entry gets saved in a list view as the title set.The list view is present in an activity say A1. Now A1 extends Activity. Also ,whenever an item in the list is “long clicked” a context menu appears, which has edit, delete and read buttons. Now if edit button is pressed I need to open another activity which can edit the data entered in the text fields corresponding to the item clicked, Also I’d be needing the id and the position of the item clicked in the list. I am using list variable of type ListView to add my adapter.Also I am checking the edit,delete and read options of the context menu in the ” public boolean onContextItemSelected(MenuItem item) ” procedure, How can I get the id and the position of the item clicked in the edit, read and delete options of the context menu?
here’s some piece of code of activity A1:
”
static id_item_clicked;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// here arg3 is the id of the item which is pressed
registerForContextMenu(arg1);
final long row_passed = arg3;
Cursor c = (Cursor) arg0.getItemAtPosition(arg2);
title = c.getString(c.getColumnIndex(DataHolder.KEY_TITLE));
story = c.getString(c.getColumnIndex(DataHolder.KEY_STORY));
........
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
id_item_clicked = arg3;
return false;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("EDIT");
menu.add("READ");
menu.add("DELETE");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getTitle() == "EDIT") {
int position = list.getSelectedItemPosition();
long item_id = list.getSelectedItemId();
}
if (item.getTitle() == "READ") {
}
if (item.getTitle() == "DELETE") {
}
return super.onContextItemSelected(item);
} "
You need to get the context menu info using
AdapterContextMenuInfo, theninfo.idwill give you the row id: