Grabbing the list item TextView value when ContextMenu is opened

给你一囗甜甜゛ 提交于 2019-12-13 01:43:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!