dException而在Android的去除ListView的项

dException而在Android的去除ListView的项

本文介绍了遇到IndexOutOfBoundException而在Android的去除ListView的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 simple_list_item_multiple_choice 列表视图在我的布局,我想从中删除所有选定的项目。我知道如何删除它,但我有在删除项目的两个主要问题: -

  1. 我的程序删除不超过2项一样,如果我选择了4个项目则只有2个将被删除,它的某个时候,甚至删除了错误的项目。

  2. 在调试我的codeI发现阵列的 IndexOutOfBoundException 在我的code和据我知道有这样的也不例外在我的code及其都是因为删错了以下的项目。

这是我的code: -

 公共无效的onClick(视图查看)
{
    SparseBooleanArray checkedPositions =新SparseBooleanArray();
    checkedPositions.clear();
    checkedPositions = lv.getCheckedItemPositions();
    INT大小= checkedPositions.size();
    如果(大小!= 0)
    {
        尝试
        {
        的for(int i = 0; I<大小;我++)
        {
            如果(checkedPositions.valueAt(i))的
            {
                list.remove(checkedPositions.keyAt(ⅰ));
                notes.notifyDataSetChanged();
                lv.setItemChecked(我,FALSE);
            }
        }}赶上(IndexOutOfBoundsException异常即)
        {}
    }
        其他{}
}
 

我只用于调试的目的引起了异常。在此先感谢,但请帮助,因为我是从最近两天停留在这个部分。

解决方案

 的for(int i =尺寸-1; I> = 0;我 - )
{
  如果(checkedPositions.valueAt(i))的
  {
    list.remove(checkedPositions.keyAt(ⅰ));
    //lv.setItemChecked(checkedPositions.keyAt(i),false);
  }
}
notes.notifyDataSetChanged();
 

I have one simple_list_item_multiple_choice listview in my layout and i am trying to remove all the selected items from it. I know how to delete it but i am having two major problems while deleting the items :-

  1. My program isn't deleting more than 2 items like if i selected 4 items then only 2 will be deleted and sometime its even deleting the wrong items.

  2. When i debug my code i found Array IndexOutOfBoundException in my code and as far as i know there is no exception like this in my code and its all because of deleting the wrong or less items.

here is my code:-

public void onClick(View view)
{
    SparseBooleanArray checkedPositions = new SparseBooleanArray();
    checkedPositions.clear();
    checkedPositions = lv.getCheckedItemPositions();
    int size = checkedPositions.size();
    if(size != 0)
    {
        try
        {
        for(int i = 0; i < size; i++)
        {
            if(checkedPositions.valueAt(i))
            {
                list.remove(checkedPositions.keyAt(i));
                notes.notifyDataSetChanged();
                lv.setItemChecked(i,false);
            }
        }}catch (IndexOutOfBoundsException ie)
        {}
    }
        else{}
}

I caught the exception only for debugging purpose. Thanks in advance but please help because i am stuck at this part since last two days.

解决方案
for(int i = size-1 ; i >= 0; i--)
{
  if(checkedPositions.valueAt(i))
  {
    list.remove(checkedPositions.keyAt(i));
    //lv.setItemChecked(checkedPositions.keyAt(i),false);
  }
}
notes.notifyDataSetChanged();

这篇关于遇到IndexOutOfBoundException而在Android的去除ListView的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:02