Detecting which selected item (in a ListView) spawned the ContextMenu (Android)

后端 未结 8 792
难免孤独
难免孤独 2020-11-27 12:27

I have a ListView that will allow the user to long-press an item to get a context menu. The problem I\'m having is in determining which ListItem they long-press

相关标签:
8条回答
  • 2020-11-27 12:35

    I do exactly this. In my onCreateContextMenu(...) method, I cast the ContextMenu.ContextMenuInfo to AdapterView.AdapterContextMenuInfo. From there, you can get the targetView, which you cast again to the widget. The complete code is available in HomeActivity.java, look for the onCreateContextMenu(...) method.

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu,
                                    View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo info =
                (AdapterView.AdapterContextMenuInfo) menuInfo;
        selectedWord = ((TextView) info.targetView).getText().toString();
        selectedWordId = info.id;
    
        contextMenu.setHeaderTitle(selectedWord);
        contextMenu.add(0, CONTEXT_MENU_EDIT_ITEM, 0, R.string.edit);
        contextMenu.add(0, CONTEXT_MENU_DELETE_ITEM, 1, R.string.delete);
    }
    

    Note that I store the selected text as well as the select id in private fields. Since the UI is thread confined, I know the selectedWord and selectedWordId fields will be correct for later actions.

    0 讨论(0)
  • 2020-11-27 12:35

    First of all, I'm wondering if you're making things a little overly complicated by using View.setOnCreateContextMenuListener(). Things get a lot easier if you use Activity.registerForContextMenu(), because then you can just use Activity.onCreateContextMenu() and Activity.onContextItemSelected() to handle all of your menu events. It basically means you don't have to define all these anonymous inner classes to handle every event; you just need to override a few Activity methods to handle these context menu events.

    Second, there's definitely easier ways to retrieve the currently selected item. All you need to do is keep a reference either to the ListView or to the Adapter used to populate it. You can use the ContextMenuInfo as an AdapterContextMenuInfo to get the position of the item; and then you can either use ListView.getItemAtPosition() or Adapter.getItem() to retrieve the Object specifically linked to what was clicked. For example, supposing I'm using Activity.onCreateContextMenu(), I could do this:

    @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);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // Here's how you can get the correct item in onContextItemSelected()
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Object item = getListAdapter().getItem(info.position);
    }
    
    0 讨论(0)
  • 2020-11-27 12:37

    I case you are using SimpleCursorAdapder you may do it like this

        @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        getActivity().getMenuInflater().inflate(R.menu.project_list_item_context, menu);
    
        // Getting long-pressed item position
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        int position = info.position;
    
        // Get all records from adapter 
        Cursor c = ((SimpleCursorAdapter)getListAdapter()).getCursor();                     
    
        // Go to required position
        c.moveToPosition(position);
    
        // Read database fields values associated with our long-pressed item
        Log.d(TAG, "Long-pressed-item with position: " + c.getPosition());
        Log.d(TAG, "Long-pressed-item _id: " + c.getString(0));
        Log.d(TAG, "Long-pressed-item Name: " + c.getString(1));
        Log.d(TAG, "Long-pressed-item Date: " + c.getString(2));
        Log.d(TAG, "Long-pressed-item Path: " + c.getString(3));
    
        // Do whatever you need here with received values
    
    }
    
    0 讨论(0)
  • 2020-11-27 12:40
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        View TargetV=(View) info.targetView;
        text1 = (String) ((TextView) TargetV.findViewById(R.id.textView1)).getText();
        text2 = (String) ((TextView) TargetV.findViewById(R.id.textView2)).getText();
        if(List3Ok){
            text3 = (String) ((TextView) TargetV.findViewById(R.id.textView3)).getText();   
        }
        selectedWord = text1 + "\n" + text2 + "\n" + text3;
        selectedWordId = info.id;
        menu.setHeaderTitle(selectedWord);
        MenuInflater inflater = this.getActivity().getMenuInflater();
        inflater.inflate(R.menu.list_menu, menu);
    }
    
    0 讨论(0)
  • 2020-11-27 12:43

    And don't forget to put this

    registerForContextMenu(listview);
    

    in your onCreate method to get your context Menu visible.

    0 讨论(0)
  • 2020-11-27 12:46

    Isn't the view argument the actual selected row's view, or am I missing the question here?

    ListView lv;
    private OnItemLongClickListener onLongClick = new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            lv.showContextMenuForChild(arg1);
            lv.showContextMenu();
            return false;
        }
    };
    
    0 讨论(0)
提交回复
热议问题