3

I'm trying to get all checked items from a list view in a Sparse boolean array and passing that array to a function which shows the user a dialog and on clicking yes does some stuff in the db for the selected items in the Sparse array.

The problem I'm facing is before calling the function I check the size of Sparse array and it shows the correct size but the function I pass it to, as soon as it gets called it shows a size of 0. I have no clue as to why that is happening.

Here is what I'm doing --->

SparseBooleanArray checkedPositions;
switch (item.getId()) {
    case delete : 
        checkedPositions = listView.getCheckedItemPositions();
        Log.d(TAG, checkedPositions.size());
        confirmDeletion(checkedPositions);
        break;
}

And this is the method I'm passing it to --->

confirmDeletion(final SparseBooleanArray checkedPositions) {
    Log.d(TAG, checkedPositions.size());
}

For the first log I get correct size but 0 in the second log. I can't figure out what's going wrong. Any help would be much appreciated.

rahul.taicho
  • 1,339
  • 1
  • 8
  • 18
  • 2
    Are you sure you are reading correct logs? If so, the only explanation is a concurrent modification. – Antoniossss Apr 03 '15 at 10:08
  • yep, I'm positive about reading correct logs. But that shouldn't be because when the logic of the function confirmDeletion() was inside the switch-case it worked smoothly but then I decided to break it down and I face this problem. I have workarounds in mind but was just curious to know why this is happening in the first place. – rahul.taicho Apr 03 '15 at 10:13
  • 2
    debug it and find out whats happening. – Ajeet47 Apr 03 '15 at 10:27
  • @AjeetKhadke Sorry for jumping the gun this is again not behaving correctly. When I debug it there are the right number of keys and values in the Sparse array but its size is 0 somehow which is really strange. Without the size I can't loop over all the keys. Any thoughts?? – rahul.taicho Apr 03 '15 at 12:33
  • 1) Are the entries you see marked as deleted? 2) Can it be accessed from more than one thread? 3) If neither of those ideas helps, strip down the program to an [SSCCE](http://sscce.org). – Patricia Shanahan Apr 03 '15 at 13:00
  • It can't be accessed from another thread and the entries have the correct key, value mapping. So I will indeed go with (3) – rahul.taicho Apr 03 '15 at 13:08

1 Answers1

1

Since there is no SSCCE I can guess that your flow is not what you think. Add stack trace to the logging so you can see who and when calls confirmDeletion() method.

Another suggestion would be assurance that you use the same object. Add toString for the logging to see sequence of the flow and that the same object is used. After all the same object does return the same result so it definitely looks like there is some gap and you are using different objects.

Community
  • 1
  • 1
Alex
  • 4,457
  • 2
  • 20
  • 59