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.
Related
so I'm currently writing an app for android, and im still a noob in java/android.
Anyways i have this 2 strings, one with names and the other with emails, and i want to output them in a listview with a custom adapter.
It works fine so far but i dont know how to set the items dynamically (with a for loop).
To create the adapter and so on, I used this tutorial:
http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
I simply changed the ImageView to a second TextView.
In the tutorials code there are 5 items added to the list, but i need them dynamically, since Im not always having the same amount of name+emails to output
I already tried putting it in a for-loop by doing:
Weather weather_data[] = new Weather[names.length];
for(int z=0; z == names.length){
Weather[z]={new Weather(names[z], emails[z])};
}
I also tried it with adding "new" infront and trying to set everything null before, basically trial&error since i dont know much about it.
So can anyone tell me how I add the items dynamically?
(Ps: sorry if I used wrong names to describe anything)
This should work
Weather weather_data[] = new Weather[names.length];
for(int z=0; z < names.length; z++){
weather_data[z] = new Weather(names[z], emails[z]);
}
Give this a read to learn how for loops work
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
and this one for arrays
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
try this
ArrayList<Weather> weatherData = new ArrayList<>();
for (int i=0; i < names.length(); i++){
weatherData.add(new Weather(names[i], emails[i]));
}
Then when you need it as a Weather[] use weatherData.toArray()
i implemented two arraylists one with thumbnails (from Images.Thumbnails) and the other one with BUCKET_DISPLAY_NAMEs of each picture (from Images.Media).
Now i want to loop through it
for (int i = 0; i < thumbnails.size(); i++) {
if (bucket_display_names.get(i).equals("WhatsApp Images")) {
......
}
}
but i get an indexoutofbounds exception in the second line, meaning that the thumbnails array is greater than the one with the bucket_display_names... aren't they supposed to contain the same amount of pictures (in fact all of the phone)?
Check the way you are getting the list of the photo gallery. Maybe the following link can help you as a starting point: Get list of photo galleries on Android
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)
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.
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.