Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse the order of list in Android?

Tags:

android

list

I am developing in Android , I read the file from the folder and put into list.

The list has two value: 1.Name 2.Time

It can show the List like the following code:

 for(int i=0;i<fileList.size();i++){
        Log.i("DownloadFileListTask", "file mName("+i+") = " + fileList.get(i).mName);
        Log.i("DownloadFileListTask", "file mTime("+i+") = " + fileList.get(i).mTime);
 }

And the log is like the following:

file mName(0) = /DCIM/100__DSC/MOV_0093.LG1
file mTime(0) = 2015-04-15 14:47:46
file mName(1) = /DCIM/100__DSC/PICT0094.JPG
file mTime(1) = 2015-04-15 14:47:52
file mName(2) = /DCIM/100__DSC/MOV_0095.LG1
file mTime(2) = 2015-04-15 14:48:04
file mName(3) = /DCIM/100__DSC/MOV_0096.LG1
file mTime(3) = 2015-04-15 14:48:12
file mName(4) = /DCIM/100__DSC/MOV_0097.LG1
file mTime(4) = 2015-04-15 14:48:20
file mName(5) = /DCIM/100__DSC/MOV_0098.LG1
file mTime(5) = 2015-04-15 14:50:26

From the log , the early time is at the first object. But I want reverse its order.

How to reverse the order of list in Android?

like image 841
Wun Avatar asked Apr 15 '15 07:04

Wun


People also ask

How do I reverse a list on Android?

Show activity on this post. Create an ArrayList of Hashmap put both values inside it, by using for loop. After that use Collections. reverse() to reverse you array value.

How do you flip an ArrayList?

To reverse an ArrayList in java, one can use Collections class reverse method i.e Collections. reverse() method. Collections reverse method reverses the element of ArrayList in linear time i.e time complexity is O(n). Collections reverse method accepts a List type as an argument.


1 Answers

Use Collections.reverse:

List myOrderedList = new List(); // any implementation
Collections.reverse(myOrderedList);
// now the list is in reverse order

Also, if you are the one adding the elements to the list, you can add the new items at the top of the list, so later you don't have to reverse it:

List<Integer> myOrderedList = new List<>(); // any implementation
myOrderedList.add(1); // adds the element at the end of the list
myOrderedList.add(0, 2); // adds the element (2) at the index (0)
myOrderedList.add(0, 3); // adds the element (3) at the index (0)
// the list is: [3, 2, 1]
like image 169
Tommaso Bertoni Avatar answered Sep 30 '22 19:09

Tommaso Bertoni