Android 检测哪个选定项(在列表视图中)生成了ContextMenu

Android 检测哪个选定项(在列表视图中)生成了ContextMenu,android,listview,Android,Listview,我在listView的每个项目中都有两个编辑文本。 当用户长按listView中的任何项目时,我会显示一个contextMenu,并提供两个选项Edit和Delete now,如何知道用户长按listView中的哪个项目打开contextMenu XML of each item of ListView <?xml version="1.0" encoding="utf-8"?> 我试着找到它,结果被跟踪了 @Override public void on

我在listView的每个项目中都有两个编辑文本。 当用户长按listView中的任何项目时,我会显示一个contextMenu,并提供两个选项Edit和Delete now,如何知道用户长按listView中的哪个项目打开contextMenu

   XML of each item of ListView

<?xml version="1.0" encoding="utf-8"?>
我试着找到它,结果被跟踪了

       @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);

// Get the info on which item was selected
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

// Get the Adapter behind your ListView (this assumes you're using
// a ListActivity; if you're not, you'll have to store the Adapter yourself
// in some way that can be accessed here.)
Adapter adapter = getListAdapter();

// Retrieve the item that was clicked on
Object item = adapter.getItem(info.position);
}

但是我不知道如何使用这个物品对象。 还有别的办法吗。
谢谢

列表视图有一个名为
getSelectedItemPosition
的函数,该函数返回一个整数,其中包含项目在适配器中的位置。我相信你可以用这个。如果在
onCreateContextMenu
中该值为空,则尝试在
onLongClick侦听器中获取对该值的引用

getSelectedItemPosition返回-1。它不适用于longClick事件。等等,您的listview没有itemLongClickListener吗?如果是这样,函数
public boolean onItemLongClick(AdapterView AdapterView,View View View,int position,long arg3)
应该为您提供longclicked项的位置。这就是你想要的吗?
   XML for context menu

 <?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/editTemplate"
          android:title="Edit" />
  <item android:id="@+id/saveTemplate"
        android:title="Save" />
  <item android:id="@+id/deleTemplate"
        android:title="Delete" />
Code


@Override 
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
          {
                    super.onCreateContextMenu(menu, v, menuInfo);
                    menu.setHeaderTitle("Select The Action");  
                    menu.add(0, v.getId(), 0, "Edit");  
                    menu.add(0, v.getId(), 0, "Delete");  


                    TextView tv=(TextView)v.findViewById(R.id.templateId);
                    selectedId=tv.getText().toString();
                    TextView tvMessage=(TextView)v.findViewById(R.id.templateTextId);
                    selectedTemplate=tvMessage.getText().toString();
                    //Toast.makeText(getApplicationContext(), "Item In List View Clicked ",Toast.LENGTH_SHORT).show();

          }


        @Override  
        public boolean onContextItemSelected(MenuItem item) {  
            if(item.getTitle()=="Edit")
            {
                       // Toast.makeText(ShowTemplates.this, "Edit Clicked",Toast.LENGTH_SHORT).show();
                        Context mContext = getApplicationContext();
                        Dialog dialog = new Dialog(ShowTemplates.this);

                        dialog.setContentView(R.layout.custome_dialog_edit_template);
                        dialog.setTitle("Edit Template");

                        txtMsgTemplate = (EditText) dialog.findViewById(R.id.editTextTemplateCustomDialog);
                        txtMsgTemplate.setText(selectedTemplate);
                        Button btnSave=(Button)dialog.findViewById(R.id.btnSaveEditedTemplate);

                        dialog.show();
              }
       @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);

// Get the info on which item was selected
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

// Get the Adapter behind your ListView (this assumes you're using
// a ListActivity; if you're not, you'll have to store the Adapter yourself
// in some way that can be accessed here.)
Adapter adapter = getListAdapter();

// Retrieve the item that was clicked on
Object item = adapter.getItem(info.position);