0

I have this script which allow to display inside a list view, the name of the files located in a specific folder.

I would like to know if is possible adapt this script to display the files without the extension and in alphabetical order. Thank you

    File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/");
    File[] filelist = dir.listFiles();
    String[] theNamesOfFiles = new String[filelist.length];
    for (int i = 0; i < theNamesOfFiles.length; i++) {
       theNamesOfFiles[i] = filelist[i].getName();
    }

    adapter = new ArrayAdapter<String>(this, R.layout.list_row, theNamesOfFiles);
    lv.setAdapter(adapter);
Bombolo
  • 749
  • 2
  • 8
  • 19
  • 1
    possible duplicate of [How to get file name without the extension?](http://stackoverflow.com/questions/924394/how-to-get-file-name-without-the-extension) – upog Mar 10 '14 at 17:29
  • For the alphabetical order you could use [Collections.sort](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html) – gtgaxiola Mar 10 '14 at 17:31

2 Answers2

1

Try this

File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/");
File[] filelist = dir.listFiles();
String[] theNamesOfFiles = new String[filelist.length];
for (int i = 0; i < theNamesOfFiles.length; i++) {
   //do a little change
   String temp = filelist[i].getName();
   theNamesOfFiles[i] = temp.substring(0,temp.length() - temp.lastIndexOf("."));
}
Arrays.sort(theNamesOfFiles)
adapter = new ArrayAdapter<String>(this, R.layout.list_row, theNamesOfFiles);
lv.setAdapter(adapter);
0

To order your file list array in alphabetical order you can use sort() static method of Arrays:

Arrays.sort(filelist);

And to remove extension you can use:

fileName = FilenameUtils.removeExtension(fileName);
stefanuc111
  • 93
  • 2
  • 4