ListView getCheckedItemPositions call issue - android

I am having issues with the getCheckedItemPositions call for ListView. The first call works but subsequent calls returned the same results even if the previous checked items were unchecked.
eg.
first call: positions 0 & 1 are checked, result shows 0 & 1 are checked
2nd call: position 0 & 1 are unchecked, result still shows 0 & 1 are checked.
Is this a bug or getCheckedItemPositions doesn't work this way? Can someone clarify please? Thanks!

Use valueAt() instead of get(), then use keyAt() to find the right index to the checked itmes works for me.
SparseBooleanArray checkedItems = lview.getCheckedItemPositions();
if (checkedItems.size() > 0) {
for (int i=0; i<checkedItems.size(); i++) {
if (checkedItems.valueAt(i)) {
Log.d("checked item: " + lview.getItemAtPosition(checkedItems.keyAt(i)));
}
}
}

Recently I had the same problem and found the answer here:
How to use getCheckedItemPositions of ListView
To get the indices of the selected items of a multi-select ListView, you can use getCheckedItemPositions() to return a SparseBooleanArray.
The function however has a trap that is not documented, is that even
you select one item and then deselect, the item is still included in
the array, although the value of the item is set to false.
So as others have said, you need to iterate thru the returned SparseBooleanArray to find the TRUE values.
Sample code in the link.

I think that is meant to be called when you are closing the list, like when it's used in a dialog, so you can save the settings.

Related

How to make Robolectric select a particular spinner item

I am new to Robolectric and am at a roadblock. I have some custom event that fires on selection of a item in spinner and i want to test that using robolectric. I saw that ShadowSpinner class provides helper function like stateSpinner.clickFirstItemContainingText to click a particular item. I populate my spinner with proper values which i test printing out each item as
for (int i = 0; i < spinner_items.length; ++i) {
spinner_items[i] = (String) spinner.getAdapter().getItem(i);
}
System.out.println("Spinner Items "
+ Arrays.asList(spinner_items).toString());
However when i use clickFirstItemContainingText with one of the po
pulated values, i get a IllegalArgumentException. No item found containing test which is strange as above code shows that particular value do exists.
Any help would be greatly appreciated
I couldn't get Robolectric to work with spinners. The problem is that the views for the list-items in the spinner won't exist apart from the one selected. This can be seen as spinner.getChildCount() will always return 1. Just call spinner.setSelection().

How to check if none of checkboxes is selected in android?

I am working on android project. I have 3 pages in which each page contains some decription and below the desciption 5 to 8 checkboxes. I kept the next button at the top of each page. I am storing the selected check box id's in an arraylist. i.e if I check the checkbox the id will be stored in the arraylist and if I uncheck the checkbox the id will be deleted from the arraylist. (I need the selected checkboxes fo further pages and hence this task). If none of the checkbox is selected and next button is clicked i want to display an alert saying that "Please select atleast one". How to check if none of the checkbox is selected and how to display an alert box? Please help me with this issue.
Thanks in advance
Loop through your ArrayList of IDs and check if each box is checked.
boolean isChecked = false;
for (int id : yourArrayOfIds) {
if (((CheckBox) findViewById(id)).isChecked()) {
isChecked = true;
break;
}
}
// If isChecked==true, a box is checked.
You may also want to check if the CheckBox object is null before using isChecked(). This is just a rudimentary example.
To show your alert, use something like this. There are lots of other tutorials and documentation as well.

Android: ArrayList Move Item to Position 0

I have an ArrayList and I need to make sure a specific item is at the 0 position and if it is not, I need to move it there. The item has an isStartItem boolean on it, so I can easily find the specific item I need to be in position 0 but then how do I go about moving it to the right position?
I am assuming I need to use something like this:
for(int i=0; i<myArray.size(); i++){
if(myArray.get(i).isStartItem()){
Collection.swap(myArray, i, 0);
}
}
But this does not seem to work...
You need to use Collections class's swap method. Collections, with an s at the end.
Change -
Collection.swap(myArray, i, 0);
to this -
Collections.swap(myArray, i, 0);
Take a look at this example.
Collection and Collections are two different things in Java. The first one is an interface, the second one is a class. The later one has a static swap method, but the former one doesn't.
I don't know what Collection.swap is, but this code should work:
for(int i=0; i<myArray.size(); i++){
if(myArray.get(i).isStartItem()){
Collections.swap(myArray, i, 0);
break;
}
}
Or you can do it long-hand:
for(int i=0; i<myArray.size(); i++){
if(myArray.get(i).isStartItem()){
Object thing = myArray.remove(i); // or whatever type is appropriate
myArray.add(0, thing);
break;
}
}
There are 2 ways of moving an item to the desired position in the ArrayList.
1. Swap the items
Collections.swap(myArray, i, 0);
--> Here position "i" will be moved to 0th position and all the other items in between this range will remain as it is.
2. Shift the items
myArray.add(0,myArray.remove(i))
--> Here item at position "i" will be removed and added to the 0th position. Here all the other items position will be shifted as you're adding a new item at 0.
Hope this will helps you to understand the difference between swap and shift the position. Use the solution according to your requirement.
You can use the set function of Arraylist.
set(position,object)

List View - scroll to bottom, WITHOUT skipping items

I have a ListView that's being populated from a custom CursorAdapter. Every item(-row) has a checkbox.
A couple of important facts before I explain the problem:
A. The checked/unchecked data is coming from the DB.
B. I save the location of every "checkbox" to a boolean array.
C. There is no way to know which checkbox is checked, without re-querying the until I scroll to it, since it is not loaded yet.
Problem is:
I am trying to create a "Clear All" button. for every "checked" checkbox, I will have to update the column in the DB to "no".
I tried LV.scrollTo(lastItem), and then check if each row is checked or not:
for (int i = 0; i < cursor.getCount(); i++) {
boolean t = itemChecked.get(i); //check the array to see if this row is checked
if (t == true) {
//set to "no" in the DB
}
but it seemed that it "skipped" over the rows between the one that were displayed on the very top and the very bottom.
I figured it's happening because they are not getting rendered, and therefore not saving the "checked" state in the array.
Then I tried this little piece of code that will scroll through every 5 rows or so, but it still didn't work:
int j = 0;
//loop times is the array.count() over the amount of rows that fit in the screen
while(j < loopTimes){
int n = j*fitToScreen;
Listview.setSelection(n);
j++ ;
}
It just jumps straight to the bottom, without reading those rows.
My guess is that there's not enough time for the rows to render.
Is there any other way of scrolling down without skipping the rows, or accomplishing what that am trying to do?
Thanks in advance.

Android: Getting a count of the visible children in a listview

Is there are way to get a count of the number of visible listview children?
I have a listview with info linked to a database that can change at any time. When the database is changed, I send out a broadcast notifying the ui class handling the list view. The child element relating to the changed data is then updated. I am achieving this by giving each listview item a tag, and then iterating over the listviews to find the row matching the tag from the broadcast.
I want to only iterate over the visible children. There is no need for me to manually update views that are not visible, as they will reflect the new data when they are created. I currently iterate from listView.getfirstVisiblePosition() to listView.getChildCount(). This is better than nothing, as I don't examine rows above the visible rows, but I don't want to examine the rows below them either.
I checked the android developers listView page and didn't find anything. Anyone know of a way I can get the count of visible children?
Thanks!
This is a quick way to get visible children count:
int visibleChildCount = (listView1.getLastVisiblePosition() - listView1.getFirstVisiblePosition()) + 1;
listView.getLastVisiblePosition(), is this what you are looking for? if not,
Iteration through child views...
int count = 0;
for (int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
if (listView.getChildAt(i) != null)
{
count++; // saying that view that counts is the one that is not null,
// because sometimes you have partially visible items....
}
}
In reference to greg7gkb's comment above - just wanted to point out in case anyone is using this that it will make your count off by one. It should be
(listView1.getLastVisiblePosition() - listView1.getFirstVisiblePosition()) + 1
So, if the last visible was 8 and the first visible was 5, you would have (8-5)+1 = 4 showing:5,6,7, and 8.
It looks like A. Abiri got it right below.

Categories

Resources