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.
Related
I am having a hard time with a piece of code that to my confidence it should work without a doubt but that is not the case.
public static void clearUnits() {
try{
for (int i = 1; i <= 3; i++) {
Log.d("S.G.E", inventoryPriceUnitsList.get(i).toString());
inventoryPriceUnitsList.remove(i);
recipePriceUnitList.remove(i);
}
}catch (Exception e){
e.printStackTrace();
}
}
The purpose of this code is to run through an array list of 4 elements and remove all elements after the first element. I know this is very basic and am sorry for wasting your time but I just needed someone else to look at this because I just don't understand why it's behaving like this. the result of this code is supposed to leave an array with one element (element 0) but instead, it leaves that and also the 3rd element. I also log all elements that are supposed to be removed and it shows up properly.
The problem is that when you remove an element from the array, the array shifts. So let's say in first round you remove the first element, then element 1 becomes element 0, and 2 becomes 1. Now on the next round you are removing the new element 1, which was the original 2, but the original 1 remains at position 0.
The simple solution is to iterate backwards, that way you are always removing elements past the point that you are at. For example
for (int i = 3; i >= 0; i--)
will work fine.
I have a problem that I don't think can be solved.
I would like to change an ImageView background that is inside a row in a ListView. The row is a custom view with two TextView and an ImageView.
I have the ListView id, the number of the row that contains the ImageView i want to edit and the ImageView id, of course.
There is any way to access the right ImageView?
Thanks for any answer.
UPDATE
Better Explanation:
I have a list of device. They are loaded from an adapter, which takes values from an array.
After the list is created, i start a new activity sending an array which contains the devices address.
The other activity receive the array, scan for devices in range (Bluetooth) and get a list of those.
Then I confront the two array and if there is a device in range, I change the image on the ListView relative to that device. The index of the array is the row number in the ListView.
I don't think I need to post any code since the array sending from an activity to the other is made via Intent and that works. Debugging I have bot the array.
I just need a way to access the image in the row of that list.
I found the solution.
I'll post here the code if someone, some day will have my problem:
// We have the listview, list of online devices and the list of our devices!
// Init ListView
ListView device_list = (ListView) findViewById(R.id.deviceListView);
// Loop for all the online devices found
for (int j = 0; j < mLeDevices.size(); j++){
// Get the online device address
BluetoothDevice device = mLeDevices.get(j);
String deviceAddress = mLeDevices.get(j).getAddress();
// Loop for all the devices in the ListView
for (int i = 0; i < device_address.size(); i++) {
// Compare devices address
if (device_address.get(i).compareTo(deviceAddress) == 0) {
int visiblePosition = device_list.getFirstVisiblePosition();
View v = device_list.getChildAt(i - visiblePosition);
ImageView state = (ImageView) v.findViewById(R.id.item_state);
state.setImageResource(R.drawable.led_on);
}
}
}
The list will have no more than 5 elements, so no problems with the list scrolling and losing the index.
I Actually would like to ask a question. Is it right to assume that the position in the ListView starts from 0? Or the first element index is 1?
If i understand your question right then you can use the following idea
If you know the position of the image view then you can try something like this in your getView method
imageView.setBackground(R.drawable.normalBackground);
if(position==yourRequiredPosition){
imageView.setBackground(R.drawable.newBackground);
}
this way it will always set normalBackground for imageView in each row, but for the yourRequiredposition row newBackground will be set. Only thing is you have to have a way to figure out "yourRequiredPosition"
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.
How will i go about saving the state of a listview item during scrolling, so that the recycler does not use it when displaying the next row being. i basically have a listview with 4 textviews and one of the textview is displayed based on a condition, derived from a database. For clarity sake, i will call that dependant textview "A".
my problem is that, simplest case: if "A" is being displayed for only the first item or any other row in the list and its not displayed anywhere else, when scrolling, "A" is displayed on other rows that should not have it. i understand the concept of listview reusing rows, but i can't figure out how to save it on an item, so it doesn't get used in another row. Here is a simplified code with just 2 textview:
holder.viewItemName = (TextView)view.findViewById(R.id.nameId);
holder.viewdescriptionStatus = (TextView)view.findViewById(R.id.viewdescriptionId);
int namecolumn = c.getColumnIndex(DBAdapter.NAME);
String name = c.getString(namecolumn);
holder.viewItemName.setText(name);
int description = c.getColumnIndex(DBAdapter.description);
String descriptionstatus = c.getString(description);
/*problem am having here*/
if(description != null){
holder.viewdescriptionStatus.setText("description available");
holder.viewdescriptionStatus.setTextColor(Color.BLUE);
}
as you can see, am using a viewHolder, but i have come to realise that viewHolder doesn't hold the logic as well. i don't know how to save the state of the holder.viewdescriptionStatus based on that condition. Most of the examples i have seen are based on checkboxes. Please anyone with ideas?.. it will be really appreciated.
P.S : i am using bindView() and newView() since i am using an SQLdatabase and SimpleCursorAdapter. i have the same issue with clicking, but i want to solve the scrolling part first. Thank you for your time.
Try this
holder.viewdescriptionStatus.setText(description != null ? "description available" : "");
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.