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)
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 am passing arraylist, which contains integer values, After checking some conditions I want to hide the previous values present in the arraylist how to achieve this one. If anyone knows please help me to resolve this.
Try this:
View v = gridview.getChildAt(ChildPosition);
v.setVisibility(GONE);
Before the attach the value to the control (setText etc.) in get view method remove that specific position or if you have specific position like you want to hide 5 number position from ArrayList than trying like
here check this condition inside get view method
if(!(position == 5))
{
mTextView.setText(String.values(bean.getValues));
}
final BookingSlotTimeAdapter bookingSlotTimeAdapter = new BookingSlotTimeAdapter(getActivity(), stringArrayList, 1);
moringGridSlot.setAdapter(bookingSlotTimeAdapter);
This way I am passing arraylist to adapter. I am checking current time as a string if current time less than passing arraylist values disable the previous times of the arraylist values.
I have
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, column));
getListView().setTextFilterEnabled(true);
and
public void onListItemClick(
ListView parent, View v, int position, long id)
I need to fine the position in column of the clicked item. Position is no good since the filter is on. Can I somehow derive the real position from 'id'?
One possible solution:
After you've set up a listview, do a once-through of the entire list, adding all the visible items indexes to an array. For instance, in a 5 element list, with the 2nd and 4th items not visible, it would look like this:
indexes[0] = 0
indexes[1] = -1
indexes[2] = 1
indexes[3] = -1
indexes[4] = 2
then when you get a position in the onListItemClick, that's just an index in the "indexes" array that returns the real position in the filtered ListView.
Then inside your onListItemClick, just use the position passed as a parameter as a key to look up the position in the filtered list.
EDIT: SparseArray is overkill. Offering reasonably easy solution.
I guess you can use .getChildCount() and .getChildAt() and then check .getVisibility() == View.VISIBLE on each to count the number of visible, and check if the clicked item is equal to the current iterrated. There's no straightforward way I'm afraid.
position in listview is the position of the element in generic object ie list you pass to an array adapter.
I haven't ever had good luck with ItemIDs.
Might want to instead pull the data from View v. This view might contain a TextView, which you could use to determine which row the user clicked on. This won't work if multiple rows can be displayed identically though...
So if that is the case, you are going to have to iterate through the dataset calling getItemID(int position) on every element in the adapter. Store these longs in an array the same size as the dataset, and use the same index on both your column dataset and this long ItemID array.
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.