How to get the position of an Item from a PopupMenu in a custom ListView

穿精又带淫゛_ 提交于 2019-12-04 05:11:30

You can create your listener class like this:

class myClickListener implements OnClickListener, OnMenuItemClickListener {

        long id;

        public myClickListener(long id) {
            this.id = id;
        }
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(appContext, v);
            popup.setOnMenuItemClickListener(this);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_item, popup.getMenu());
            popup.show();
        }
        @Override
        public boolean onMenuItemClick(MenuItem arg0) {
            Toast.makeText(appContext, "ID of selected row : " + id , Toast.LENGTH_SHORT).show();
            return false;
        }

    }

and add this listener to your imageview at your override getView method of adapter class:

imageView.setOnClickListener(new myClickListener(getItemId(position)));

Simply we need to set buttons tag as its position, and onClick on the PopupButton you need to get the view:

button.setTag(position);  //may be you can call this on getview method of list view
button.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) {
        int position = (Integer) v.getTag();   // you will get the position

    }
});

or use Interface in your fragment implements View.OnClickListener

private void showPopupMenu(View view) {

    final   int position = (Integer) view.getTag();

        PopupMenu popup = new PopupMenu(getActivity(), view);

        // Inflate our menu resource into the PopupMenu's Menu
        popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

        // Set a listener so we are notified if a menu item is clicked
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                case R.id.menu_remove:


                System.out.println(position);
                    return true;
                }
                return false;
            }
        });

        // Finally show the PopupMenu
        popup.show();
    }

There is no way to achieve what you want with a onClick declared by XML. The easiest way to solve it with your actual implementation is to :

  • add a real click listener to your ImageView after inflating it. Inside your adapter.
  • the listener should be a custom class implementing the OnClickListener interface
  • you should also give your listener an additional data member : the position (row) that contains the ImageView.
  • when the listener is activated, it will have to invoke a callback on the activity that will display the Dialog, the listener will then pass the position.

Here's how I did it:

  1. Instead of storing a ViewHolder in the tags of convertView in CustomArrayAdapter's getView(int position, View convertView, ViewGroup parent) method, I stored the Custom object itself. I didn't even use a holder.

    convertView.setTag(getItem(position));
    

    (if you really need to store the ViewHolder, you can use setTag(int key, Object tag) to store multiple tags)

  2. When showMenu(View view) is called by the ImageView, I use view.getParent() to get the parent LinearLayout of the currently selected view, which has my Custom object in its tag.

    View parent = (View) view.getParent();
    
  3. Outside showMenu() I have defined a public property to store the currently selected item like this:

    public static Custom mSelectedItem;
    
  4. Inside showMenu() I set mSelectedItem to getTag() of the parent LinearLayout from step 2.

    mSelectedItem = (Custom) parent.getTag();
    
  5. Finally, in the onMenuItemClick(MenuItem menuItem) override method I use mSelectedItem to determine the currently selected item.

    Custom custom = mSelectedItem;
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!