删除列表视图的最后一项 [英] Removing last item of a listview

查看:84
本文介绍了删除列表视图的最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击按钮以删除列表视图中的最后一项时,它没有显示错误,但应用程序崩溃.列表视图可以与arraylist配合使用……只是当我想删除最后一个项目时,它使我崩溃了.

It shows no error but the app crash when i click the button to remove the last item in the list view. List view works fine with the arraylist... just when i want to remove the last item, it gives me a crash.

btnUndo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int count = adapter.getCount();
            adapter.remove(adapter.getItem(count));
            adapter.notifyDataSetChanged();
        }
    });

推荐答案

数组是基于0的,因此您应该执行以下操作:

Arrays are 0-based, so you should do the following:

adapter.remove(adapter.getItem(count - 1));

我还建议使用notifyItemRemoved 而不是notifyDataSetChanged.

I also suggest to use notifyItemRemoved instead of notifyDataSetChanged.

这篇关于删除列表视图的最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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