簡體   English   中英

檢測哪個選擇的項目(在ListView中)產生了ContextMenu

[英]Detecting which selected item (in a ListView) spawned the ContextMenu

我在listView的每個項目中都有兩個Edit Text。 當用戶長按listView中的任何項目時,我將顯示一個contextMenu並提供兩個選項,現在如何編輯和刪除,我如何知道用戶長按listView中的哪個項目以打開上下文菜單。

   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對象。 還有其他方法可以做到這一點。 謝謝

ListViews具有一個名為getSelectedItemPosition的函數,該函數返回一個帶有項目在適配器中位置的int。 我很確定您可以使用它。 如果在onCreateContextMenu中為null,則嘗試在onLongClick listener獲取對其的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM