Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ListView - Context menu not working

I have a ListFragment where I register the context menu using registerForContextMenu() and I override onCreateContextMenu(). The issue is that onCreateContextMenu() is never called when I long press any item.

Here's some code:

public class List_F extends ListFragment {
    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);

        registerForContextMenu(getListView());

        setListAdapter(...);
        setListShown(false);

        // launch cursor loader
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int i = item.getItemId();
        if (i == R.id.menu_item_delete) {
            delete(info.id);
            return true;
        } else {
            return super.onContextItemSelected(item);
        }
    }
}

Any idea why?

I also tried

getListView().setOnItemLongClickListener() 

and it's not even called.

Any idea?

like image 941
Simon Reggiani Avatar asked Feb 14 '26 10:02

Simon Reggiani


1 Answers

Found the culprit!

I was setting a OnClickListener on the view in the Adapter.

This was probably eating the long press event.

I changed the OnClickListener in each view to a OnItemClickListener on the ListView

like image 95
Simon Reggiani Avatar answered Feb 15 '26 23:02

Simon Reggiani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!