Why is item.getMenuInfo() null?

后端 未结 3 1320
梦如初夏
梦如初夏 2021-01-24 21:38

Within my ListVeiw item I have 4-8 ImageView(some of them are invisible depended of some value) and 4 TextView.

When I Long click to item of Listview the (AdapterContext

相关标签:
3条回答
  • 2021-01-24 21:54

    It may not have anything to do with it, but one glaring thing I see right away is:

    public Object getItem(int position) {
       return position;
    }
    
    public long getItemId(int position) {
       return position;
    }
    

    Your getItem method is returning an int of the object's position, not the actual Object itself as stated in its method declaration.

    0 讨论(0)
  • 2021-01-24 22:01

    getMenuInfo() will work on ListAdapter, not on views.

    But, You can pass additional data with the tag of the view.

    in getView: vi.setTag(position) activity.registerForContextMenu(vi);

    declare in Activity private int id;

    onCreateContextMenu: id = (int) v.getTag();

    onContextItemSelected: you can use the id

    0 讨论(0)
  • 2021-01-24 22:07

    You need to register the list for context menu, not the items.

    // wrong:
    activity.registerForContextMenu(vi);
    
    // right:
    activity.registerForContextMenu(myList);
    
    0 讨论(0)
提交回复
热议问题