【问题标题】:How to display name of the files without extensions and in alphabetical order如何按字母顺序显示不带扩展名的文件名
【发布时间】:2014-03-10 17:27:56
【问题描述】:

我有这个脚本,它允许在列表视图中显示位于特定文件夹中的文件的名称。

我想知道是否可以调整此脚本以显示不带扩展名并按字母顺序排列的文件。 谢谢

    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);

【问题讨论】:

标签: java android file


【解决方案1】:

试试这个

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);

【讨论】:

  • 你好,迪安。您的代码可以完美地按字母顺序对名称进行排序。对于文件扩展名,它只是删除最后一个字母
  • 似乎每个字符串不能返回超过4个字母
【解决方案2】:

要按字母顺序排列文件列表数组,您可以使用数组的 sort() 静态方法:

Arrays.sort(filelist);

要删除扩展,您可以使用:

fileName = FilenameUtils.removeExtension(fileName);

【讨论】:

  • 感谢您的回答。不幸的是,我似乎在 Eclipse 中没有这个 apache 包
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 2015-03-11
相关资源
最近更新 更多