如何动态上的一个按钮点击删除ListView的项目? [英] How to dynamically remove items from ListView on a button click?

查看:127
本文介绍了如何动态上的一个按钮点击删除ListView的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,需要从的ListView 上的一个按钮事件中删除项目。

I am working on an app that requires to remove items from a ListView on a button event.

我试图从的ArrayList中删除并创造全新的适配器,并再次加载列表中,但我的名单是巨大的,会影响我的应用程序的性能,所以我不知道是否有任何其他方式,我可以从我的动态列表中删除项目?

I tried to remove it from ArrayList and create the whole new adapter and loaded the list again but as my list is huge it will affect performance of my app so I was wondering if there is any other way by which I could remove item from my list dynamically?

我想不管你说的家伙。

I tried whatever you said guys.

起初,我只是想删除一个项目,这是工作完美的,但因为我是它开始给我IndexOutOfBoundException增加所选项目的数量。

At first i just tried to remove one item and it was working perfect but as i am increasing the number of selected items it starts giving me IndexOutOfBoundException.

下面是我的code: -

Here's my code :-

public void onClick(View view)
{
    SparseBooleanArray checkedPositions = new SparseBooleanArray();
    checkedPositions.clear();
    checkedPositions = lv.getCheckedItemPositions();
    int size = checkedPositions.size();
    if(size != 0)
    {

        for(int i = 0; i <= size; i++)
        {
            if(checkedPositions.valueAt(i))
            {
                list.remove(notes.getItem(checkedPositions.keyAt(i)));
                notes.notifyDataSetChanged();
            }
        }
    }
        else{}
}

下面Notes是一个参考SimpleAdapter的对象。

Here notes is a reference to an object of SimpleAdapter.

推荐答案

嗯,你刚刚从列表中使用<$的删除()方法去除所需的项目C $ C> ArrayAdapter 。

Well you just remove the desired item from the list using the remove() method of your ArrayAdapter.

一个可能的方式做到这一点是:

A possible way to do that would be:

Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);

另一种方法是修改的ArrayList 和呼叫 notifyDataSetChanged() ArrayAdapter

arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();

这篇关于如何动态上的一个按钮点击删除ListView的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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