1

I am really frustrated because I have been trying this for 4 hours today, and I can't find a solution. For some reason, my on long click listener doesn't get detected sometimes!

 listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                       int pos, long id) {

            Toast.makeText(MainActivity.this, "Long click", Toast.LENGTH_SHORT).show();

            contactArrayList.remove(pos);
            arrayAdapter.notifyDataSetChanged();


            return true;
        }
    }); 

This simple code just removes an item from my list and my array list using an adapter. But, sometimes, the onItemLongClickListener doesn't even get called, and the toast doesn't even display!! I know that android is detecting it, because I get this message whenever I long click in my log cat:

/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP

So why does it only work sometimes? Is there something wrong with my code, or is this a problem with android itself? If it is a problem with android, how can I fix it?

It works the first time, but after exiting my app, rotating the screen, etc. , Long press stops getting detected. I have been stuck on this for almost 8 hours now, and I really, really, really need your help.

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • I don't know if this is releated, but I also got this message: `Internal data leak within a DataBuffer object detected! Be sure to explicitly call release() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@RANDOMNUMBERSANDLETTERS)` – Ruchir Baronia Jan 31 '16 at 00:33
  • Also, for example it will work, then I will rotate the screen, and it will stop working... – Ruchir Baronia Jan 31 '16 at 00:42
  • there is nothing wrong with the code posted. Checking your stacktrace and checking for memory leaks might help find the cause – Raghunandan Jan 31 '16 at 06:44
  • @Rich, I have few questions, in which method you are registering for listener, is any condition applied for setting listener. If it is got fixed please ignore this. – vinay kumar Feb 01 '16 at 03:59
  • 2
    Possible duplicate of [OnItemCLickListener not working in listview](https://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview) – Winter Sep 13 '17 at 14:35

3 Answers3

0

May be try using View.OnItemLongClickListener() instead of AdapterView and change the parameter in OnItemLongClick() to View.

Hope this helps...

GauRavK
  • 31
  • 1
  • 4
0

Check if you're actually removing the item from the array inside the adapter.

cmak
  • 576
  • 1
  • 4
  • 15
0

Sounds like your listener gets destroyed during a config change, or does not always get initialized. Make sure the code block which setting the listener is part of gets called each time.

breakline
  • 5,776
  • 8
  • 45
  • 84