Android:如何找到从上下文菜单中单击的位置

Android: How to find the position clicked from the context menu(Android:如何找到从上下文菜单中单击的位置)

本文介绍了Android:如何找到从上下文菜单中单击的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满数据的列表视图.我使用以下代码为 listview 设置了一个上下文菜单:

I have a list view filled with data. I set up a context menu for the listview using the following code:

list.setOnCreateContextMenuListener
(
  new View.OnCreateContextMenuListener() 
  {
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) 
        {
       AdapterContextMenuInfo mi =(AdapterContextMenuInfo) menuInfo;
       menu.add(0, 0, 0, "Delete item");                
    }
   }
);

我有以下方法覆盖来控制选中的上下文菜单菜单项:

I have the following method override to control de contextmenu menuitem selected:

@Override
public boolean onContextItemSelected(MenuItem item) 
{
  switch(item.getItemId()) 
  { 
  case 0: 
    ShowAlert("hello from delete item");
    break; 
  default: 
  return super.onContextItemSelected(item); 
  } 
  return true; 
}

在这个被覆盖的方法中,我怎样才能找到被点击的列表视图的项目?

In this overridden method, how could I find the item of the list view that was clicked?

推荐答案

可以使用ContextMenu.ContextMenuInfo.

类似的东西:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
}

您还可以获得正在显示菜单的确切 View:

You can also get the exact View for which the menu is being displayed:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
    View view = info.targetView;
}

这篇关于Android:如何找到从上下文菜单中单击的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android:如何找到从上下文菜单中单击的位置

基础教程推荐