I have a list view connected to a database, showing a all the entries. I want a menu to show up if the user long clicks a item in the listview(database entry), showing options to edit or delete the entry. how can i do this.
Till now, I have tried using a onItemLongClick listener and a toast in it showing the id of the view long clicked.
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
String res = Long.toString(id);
Toast toast = Toast.makeText(this, res, Toast.LENGTH_SHORT);
toast.show();
return true;
}
**
** override onCreateContextMenu Method like this
then coding for each item that can be selected
Another approach:
First you need to register your context menu on list view.
Then, just override activity methods.
Here is an example of menu_list.xml file (you have to put the file in the res/menu folder).
Hope it will help.
You can call
Activity.openOptionsMenu()
in your click item methodcheck here
http://developer.android.com/reference/android/app/Activity.html#openOptionsMenu%28%29
Use registerForContextMenu(); to link context menu to any View successor.
To access to selected ListItem data, simple use the AdapterView.AdapterContextMenuInfo. E.g.:
Instead of using
onItemLongClick
you can usewhere you setup the options for edit and delete or whatever you need to.
The actions for the item selected from the context menu can be processed in
For more information on context menu see here.
For a step by step tutorial visit here.