简体   繁体   中英

Why can't I remove an item?

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.

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

Hope this helps...

I am getting a similar issue where my long Click on a list item doesn't get recognized for one particular case.

It's happening for me when my list item text view contains a click-able hyperlink. I am able to click the hyperlink and android redirects me to a browser but my long click/ single click doesn't get recognized for the whole item selection in this case and hence my Actionmode also doesn't work.

I am trying to solve it through the following links, hope it helps

link 1

link 2

检查您是否真的从适配器内的数组中删除了该项目。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM