简体   繁体   English

为什么我无法删除项目?

[英]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.我真的很沮丧,因为我今天已经尝试了 4 个小时,但找不到解决方案。 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!!但是,有时,甚至没有调用 onItemLongClickListener,甚至没有显示吐司! I know that android is detecting it, because I get this message whenever I long click in my log cat:我知道 android 正在检测它,因为每当我长按日志猫时都会收到此消息:

/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?我的代码有问题吗,或者这是android本身的问题? If it is a problem with android, how can I fix it?如果是android的问题,我该如何解决?

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.我已经被困在这个问题上将近 8 个小时了,我真的、真的、真的需要你的帮助。

May be try using View.OnItemLongClickListener() instead of AdapterView and change the parameter in OnItemLongClick() to View. 可以尝试使用View.OnItemLongClickListener()而不是AdapterView,并将OnItemLongClick()中的参数更改为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. 我能够单击超链接,而android将我重定向到浏览器,但是在这种情况下,我的长按/单击无法识别整个项目,因此我的Actionmode也无法使用。

I am trying to solve it through the following links, hope it helps 我正在尝试通过以下链接解决此问题,希望对您有所帮助

link 1 链接1

link 2 连结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.确保每次都调用设置侦听器所属的代码块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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