为什么我无法删除项目? [英] Why can't I remove an item?

查看:27
本文介绍了为什么我无法删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很沮丧,因为我今天已经尝试了 4 个小时,但找不到解决方案.出于某种原因,我的长按监听器有时没有被检测到!

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;
        }
    }); 

这个简单的代码只是使用适配器从我的列表和数组列表中删除了一个项目.但是,有时,onItemLongClickListener 甚至没有被调用,toast 甚至没有显示!! 我知道 android 正在检测它,因为每当我在日志中长按时我都会收到此消息猫:

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

那为什么它只是有时有效?我的代码有问题吗,或者这是android本身的问题?如果是android的问题,我该如何解决?

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?

它第一次工作,但在退出我的应用程序、旋转屏幕等后,长按停止被检测到.我已经坚持了将近 8 个小时,我真的、真的、真的需要你的帮助.

推荐答案

听起来像是您的侦听器在配置更改期间被破坏,或者并不总是被初始化.确保每次都调用设置侦听器所在的代码块.

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.

这篇关于为什么我无法删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆