Detecting Which Selected Item (In a Listview) Spawned the Contextmenu (Android)

Detecting which selected item (in a ListView) spawned the ContextMenu (Android)

I do exactly this. In my onCreateContextMenu(...) method, I cast the ContextMenu.ContextMenuInfo to AdapterView.AdapterContextMenuInfo. From there, you can get the targetView, which you cast again to the widget. The complete code is available in HomeActivity.java, look for the onCreateContextMenu(...) method.

@Override
public void onCreateContextMenu(ContextMenu contextMenu,
View v,
ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) menuInfo;
selectedWord = ((TextView) info.targetView).getText().toString();
selectedWordId = info.id;

contextMenu.setHeaderTitle(selectedWord);
contextMenu.add(0, CONTEXT_MENU_EDIT_ITEM, 0, R.string.edit);
contextMenu.add(0, CONTEXT_MENU_DELETE_ITEM, 1, R.string.delete);
}

Note that I store the selected text as well as the select id in private fields. Since the UI is thread confined, I know the selectedWord and selectedWordId fields will be correct for later actions.

Find out which item in the ListView invoked the context menu in Android

I found out that the ListView has to be registered for the context menu.

Here is the code that I used:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

super.onCreateContextMenu(menu, v, menuInfo);
ListView lv = (ListView) v;
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
String contextMenuInvoked = (String) lv.getItemAtPosition(acmi.position);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Edit");
}

Identifying the view selected in a ContextMenu (Android)

There is no such concept as "identifying the View selected" for either option menus or context menus in Android. Hence, it is rather difficult to answer your question. So, I'll take some guesses.

If by "identifying the View selected" you mean which menu choice was selected, that is getItemId() on the MenuItem that is passed to onOptionsItemSelected() or onContextItemSelected().

If by "identifying the View selected" you mean which row in a ListView was the one long-tapped on to bring up the context menu, cast getMenuInfo() (called on the MenuItem) to AdapterView.AdapterContextMenuInfo, then use either the id or the position values as appropriate based on your adapter. See here for a sample project that uses this technique.

If by "identifying the View selected" you mean you have more than one non-ListView context menu in an activity, I would not use that UI technique.

Set title of context menu from the selected Listview item

Use the ContextMenuInfo parameter from the onCreateContextMenu() method:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info;
try {
// Casts the incoming data object into the type for AdapterView objects.
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
// If the menu object can't be cast, logs an error.
Log.e(TAG, "bad menuInfo", e);
return;
}
Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
if (cursor == null) {
// For some reason the requested item isn't available, do nothing
return;
}

// if your column name is "name"
menu.setHeaderTitle(cursor.getString(cursor.getColumnIndex("name")));
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}

Information from listview row for Android context menu

Start here:

Detecting which selected item (in a ListView) spawned the ContextMenu (Android)

How do you implement context menu in a ListActivity on Android?

Also, here is a sample project that demonstrates getting the _ID of a database row in support of a "Delete" context menu item.

Getting id of selected item in listview

Use this

AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item .getMenuInfo();   
int positionSelected = menuInfo.position;

How to get the Button view which triggered a Context Menu?

The Button that was used to create the context menu is the View parameter passed into onCreateContextMenu.



Related Topics



Leave a reply



Submit