Change the item index in an arraylist

43
September 03, 2019, at 11:40 AM

I have a List<POJO> with three Packagenames. So if I "click" on the second item com.app.2 I want that the index is 0. I don't want to replace it, I want to change the index only.

For example:

(0) com.app.1
(1) com.app.2
(2) com.app.3

Click on the second item (from above)

(0) com.app.2
(1) com.app.1
(2) com.app.3

Click on the third item (from above)

(0) com.app.3
(1) com.app.2
(2) com.app.1

Click on the second item (from above)

(0) com.app.2
(1) com.app.3
(2) com.app.1

Well I thought List<POJO>.set(i,Object) will do it, but it's doing something like this:

(0) com.app.1
(1) com.app.2
(2) com.app.3

Click on the second item (from above)

(0) com.app.2
(1) com.app.2
(2) com.app.3

So it is replacing the item and thats not what I want.

Answer 1

Try combining remove(int) with add(int, E):

POJO tmp = list.remove(i); // the clicked index
list.add(0, tmp);

In this particular case, everything works perfectly. However, if you were adding to a position other than 0 you'd have to keep in mind that everything in the list after the removed item will "slide" up one position.

READ ALSO
Unity IL2CPP Android build crashes Mac

Unity IL2CPP Android build crashes Mac

When trying to build a Google App Bundle, every time the process gets to the step that says Building Native Binaries With IL2CPP I get a “Your system has run out of application memory” error and my MacBook reboots

24
Android Retrofit: async loading for model data to be filled into spinner issue

Android Retrofit: async loading for model data to be filled into spinner issue

so I'll be straight forward, I have a model class, that will be loading from the database using retrofit asynchronously, the data loaded will be displayed in a spinnerthe issue is: since it's loading asynchronously, the spinner is being filled with an empty arraylist,...

53
How to lock launcher to try uninstall an app? is it possible to disable launcher by uninstall an app like play store? [on hold]

How to lock launcher to try uninstall an app? is it possible to disable launcher by uninstall an app like play store? [on hold]

I would like to block launcher by uninstall an application if I cannot insert a passwordHow can I do it?

34
Using the Mapbox Places Plugin, how to create proximity Point of current user&#39;s location?

Using the Mapbox Places Plugin, how to create proximity Point of current user's location?

I'm trying to customize my autocomplete search results to places that are closer to the user's current location, however I'm stuck on creating a proximity Point to put into proximity()In other words, I want to create a Point of the user's current location

52