-1

I have seen other post but can't seem to put it well in my post. Could someone help me with this.i am able to view the files and open them but need them to list from the recent create to oldest. Below is my code.

    private List<String> fileList = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   String root = Environment.getExternalStoragePublicDirectory("/MyAppFolder").
            getAbsolutePath();
    // ListDir(root);

    pdf = new File(root);
    ListDir(pdf);
   }

    void ListDir(File f) {
    File[] files = f.listFiles();
    fileList.clear();

    for (File file : files) {



        fileList.add(file.getName());

    }



    ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, fileList);

    setListAdapter(directoryList);

  }

  public void onListItemClick(ListView parent, View v, int position, long id) {
    //selection.setText(fileList.indexOf(simple_list_item_1));
    OpenPdf(fileList.get(position).toString());
  }



  public void OpenPdf(String path)
  {
      File file = new File(path);
      if (file.exists()) {
          Uri p = Uri.fromFile(file);
          Intent intent = new Intent(Intent.ACTION_VIEW);
          intent.setDataAndType(p, "application/pdf");
          intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

          try {
              startActivity(intent);
          } 
          catch (ActivityNotFoundException e){
          }
      }
  }
}
ElGavilan
  • 6,610
  • 16
  • 27
  • 36
Ahsan
  • 195
  • 1
  • 20
  • this duplicate is the first in the related questions list. a little research effort please. – njzk2 May 21 '14 at 16:08

2 Answers2

0

You can checkout the following question this shows how to do it for the last mod timestamp. It should be easily adaptable for your case.

The most interesting part here is the Comparator.

Community
  • 1
  • 1
B4dT0bi
  • 623
  • 4
  • 21
  • this is the error i am getting-----Multiple markers at this line - The method sort(File[], new Comparator(){}) is undefined for the type List - Comparator is a raw type. References to generic type Comparator should be parameterized – Ahsan May 21 '14 at 16:06
  • you need to write your own Comparator. – B4dT0bi May 21 '14 at 16:09
  • here, i tried that. Not really good with comparator but this is what i got..... fileList.sort( files, new Comparator() { public int compare(Object o1, Object o2) { if (((File)o1).lastModified() > ((File)o2).lastModified()) { return -1; } else if (((File)o1).lastModified() < ((File)o2).lastModified()) { return +1; } else { return 0; } } }); – Ahsan May 21 '14 at 16:24
0
Comparator<File> fileDateCmp = new Comparator<File>() {
    public int compare(File f1, File f2) {
        return Long.compare(f1.lastModified(), f2.lastModified());
    }
};


File[] yourArrayOfFiles
Arrays.sort(yourArrayOfFiles, fileDateCmp);

For instance on my school computer:

    Comparator<File> fileDateCmp = new Comparator<File>() {
        public int compare(File f1, File f2) {
            return Long.compare(f1.lastModified(), f2.lastModified());
        }
    };

    File[] yourArrayOfFiles = File.listRoots();

    System.out.println(Arrays.toString(yourArrayOfFiles));

    Arrays.sort(yourArrayOfFiles, fileDateCmp);
    System.out.println(Arrays.toString(yourArrayOfFiles));
    for(File file : yourArrayOfFiles) {
        System.out.print(file + ": ");
        System.out.println(file.lastModified());
    }

Output:

[C:\, D:\, K:\, M:\, P:\, S:\, U:\, V:\]
[D:\, V:\, M:\, K:\, P:\, U:\, S:\, C:\]
D:\: 0
V:\: 1370008587158
M:\: 1386966108069
K:\: 1395062748515
P:\: 1396364590821
U:\: 1398716910066
S:\: 1400518034257
C:\: 1400684450266
DirkyJerky
  • 1,130
  • 5
  • 10