我有此脚本,该脚本允许在列表视图中显示位于特定文件夹中的文件的名称。
我想知道是否可以修改此脚本以显示不带扩展名的文件并按字母顺序排列。
谢谢
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);
最佳答案
尝试这个
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);