0

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.

Varundroid
  • 9,135
  • 14
  • 63
  • 93
  • should be i – m0s Apr 01 '11 at 07:15
  • @m0s i tried that too i even tried i = 1, size + 1, size -1 or every possible change could be made to the loop. still not working. Exception is still there. :( – Varundroid Apr 01 '11 at 07:18
  • you should check what causes the exception, the list.remove(...) or the checkedPositions.valueAt(...). And Its not very clear what is list for and how its been initialized – m0s Apr 01 '11 at 07:24
  • m0s i have posted more code take a look if you cound find an error and i just found something, may be it could help you to find the error ; keyAt() :- Given an index in the range 0...size()-1, returns the key from the indexth key-value mapping that this SparseBooleanArray stores. may be because of this size()-1 i am having this problem. what do you think? – Varundroid Apr 01 '11 at 07:35

3 Answers3

3

Each time you remove an item from the array at the lower lever, the total count is being reduced by 1. If you had 4 items to remove [ 0, 1, 2, 3], and you remove items starting with item 0, you have [0, 1, 2], then you remove item at 1, and you have [0, 1], if you try to remove item at index 2 which does not exist you will get an error. Try count down instead of up like this

for(int i = size; i > 0; --i)
{
  if(checkedPositions.valueAt(i))
  {
    list.remove(checkedPositions.keyAt(i));
    notes.notifyDataSetChanged();
    lv.setItemChecked(i,false);
  }
}
bdhac
  • 359
  • 4
  • 5
  • ok as u suggest i tried it but when i tried to remove three items it just deleted two and when i tried to delete four items then encountered IOOBException again and had to force close my app. :( – Varundroid Apr 01 '11 at 07:46
  • Try to comment out this line and see if it is causing the error //lv.setItemChecked(i,false); – bdhac Apr 01 '11 at 07:54
  • Tried it bt still getting this java.lang.IndexOutOfBoundsException: Invalid index 11, size is 11 – Varundroid Apr 01 '11 at 08:00
  • ok i tried something stupid and it seems its the best working attempt till now what i did i passed i>-1 to the loop and everything was working perfect till the time i tried to remove last two items left. this time it gave me java.lang.IndexOutOfBoundsException: Invalid index 4, size is 2 any advise? – Varundroid Apr 01 '11 at 08:12
1

From the look of it, you should change this

for(int i = 0; i <= size; i++)

to

for(int i = 0; i < size; i++)
SteD
  • 13,909
  • 12
  • 65
  • 76
  • Sted i tried that too still not working. i tried every possible change could be made to that loop. :( – Varundroid Apr 01 '11 at 07:20
  • which line is exactly giving you the IndexOutOfBoundException? Does it happen at list.remove(checkedPositions.keyAt(i)) ? – SteD Apr 01 '11 at 07:23
  • yes SteD i am having error in this line because keyAt() returns size()-1 index and that could be a problem in my code for sure but i don't know how to remove this error. Any solution? – Varundroid Apr 01 '11 at 07:41
1
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();
kharles
  • 326
  • 5
  • 14
  • i think sometimes your IndexOutOfBoundsException is happening at with the checkedPositions.keyAt(i) call [if you initialized your loop wrong] and sometimes it is with the list.remove(#blah#) call [if you removed some elements already] which is making this confusing to debug. – kharles Apr 01 '11 at 09:13
  • if this still doesn't work try doing something like: int temp = checkedPositions.keyAt(i); list.remove(temp); so that you can debug which part is crapping out. – kharles Apr 01 '11 at 09:15
  • @Kharles man your solution was working like charm but as i tried to remove all the items at once, its again start giving me IOOBException. – Varundroid Apr 01 '11 at 09:25
  • its probably that last line: lv.setItemChecked(checkedPositions.keyAt(i),false); comment that out for now and it shouldn't error – kharles Apr 01 '11 at 09:28
  • if you're removing the item, you probably wouldn't need to uncheck it? – kharles Apr 01 '11 at 09:28
  • which line exactly is it crapping out on? – kharles Apr 01 '11 at 09:31
  • same as you mentioned its crapping out at list.remove(temp). and i removed lv.setItemChecked(checkedPositions.keyAt(i),false) still its throwing IOOBE at list.remove(temp). – Varundroid Apr 01 '11 at 09:40
  • the only error remained is to delete all rows at once otherwise your solution is working like charm. – Varundroid Apr 01 '11 at 09:41
  • move notes.notifyDataSetChanged(); to outside the for loop... you shouldn't be calling it until you've done all your updates anyway. i'm guessing it might be screwing some indices up? – kharles Apr 01 '11 at 09:45
  • tried as you said i put notifyDataSetChanged out of loop but still IOOBException. i think its a bug in android. – Varundroid Apr 01 '11 at 09:55
  • java.lang.IndexOutOfBoundsException: Invalid index #, size is #... what are the # for this particular crash, just curious – kharles Apr 01 '11 at 09:57
  • java.lang.IndexOutOfBoundsException: Invalid index 6, size is 6 – Varundroid Apr 01 '11 at 09:58
  • oh, do you have a dummy element at position 0 in the listview or something? verify that this crashes if you only select the last element (and even if the only last element in the list). if that's true then do remove(temp-1) or something like that – kharles Apr 01 '11 at 10:02
  • yeah i got the error now, i am using one more checkbox to check all these items at once and this checkbox isn't a part of listview. It is defined in my main.xml and when i am getting the all checked items may be this checkbox's reference was delivered as a part of those items. I came to this conclusion because when i checked all items one by one it doesn't throw any exception and when i am trying to select all items using this independent checkbox then its throwing an exception. What do you think am i right? – Varundroid Apr 01 '11 at 10:08
  • yea something about your using the checkbox makes all the values returned off by 1 too many. (it makes the checkbox be pos 0 or something.) good luck, man. i think we've pinpointed the issue finally after fixing the other 2 sources of OOB exceptions you had. – kharles Apr 01 '11 at 10:11
  • lol i am just switching from one error to another. I don't know am i gonna complete this app at given time. thanks man you helped me a lot. – Varundroid Apr 01 '11 at 10:17
  • nah, we've gone from 3 errors down to 1 error. the combination of the 3 at once was too confusing. no problem. good luck. – kharles Apr 01 '11 at 10:20