0

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

   XML of each item of ListView

<?xml version="1.0" encoding="utf-8"?>

    <TextView android:id="@+id/templateId"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>

    <TextView android:id="@+id/templateTextId"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>

   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);

}

但我不知道如何使用这个 Item 对象。有没有其他方法可以做到这一点。谢谢

4

1 回答 1

1

ListViews 有一个调用的函数getSelectedItemPosition,它返回一个 int,其中包含项目在适配器中的位置。我很确定你可以使用它。如果那是空的,onCreateContextMenu那么尝试在你的onLongClick listener.

于 2012-07-31T06:02:24.390 回答