问题
On a ListView single/short click, I can do this:
protected void onListItemClick(ListView listView, View v, int position,
long id) {
tvInt = reviews.get(position);
}
How would I do this for a ContextMenu? My ListView simply contains a single TextView.
Edit: I want to grab the value of the TextView in the ListView, not the ContextMenu.
回答1:
The MenuItem packs extra information from where you could extract the position of the clicked row in the ListView and then simply use the code you used in the onListItemClick callback:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int clickedPosition = info.position;
tvInt = reviews.get(position);
// ...
The same information, ContextMenuInfo, is available in the onCreateContextMenu callback if you want to get the String when building the ContextMenu.
来源:https://stackoverflow.com/questions/11321238/grabbing-the-list-item-textview-value-when-contextmenu-is-opened