Showing a list of files in a ListView

后端 未结 2 912
北恋
北恋 2020-12-17 01:17

I\'m wondering how to show files from a directory in a ListView`. The files can be listed with:

File dir = new File(dirPath);
File[] filelist = dir.listFiles         


        
相关标签:
2条回答
  • 2020-12-17 01:51

    I guess you want to show the names of the files from that directory so you could try this:

    File dir = new File(dirPath);
    File[] filelist = dir.listFiles();
    String[] theNamesOfFiles = new String[filelist.length];
    for (int i = 0; i < theNamesOfFiles.length; i++) {
       theNamesOfFiles[i] = filelist[i].getName();
    }
    

    The adapter to use with the list :

    new ArrayAdapter<String>(this, android.R.layout.simple_list_item, theNamesOfFiles);
    

    For anything more complicated than showing the names of the files you have to implement a custom adapter.

    0 讨论(0)
  • 2020-12-17 01:56

    Or you can use something like this for a sorted String of filenames:

    File dataDirectory = Environment.getDataDirectory();
    File fileDir = new File(dataDirectory, "data/com.yourapp.app/files");
    
    String[] listItems = fileDir.list();
    Arrays.sort(listItems);
    
    0 讨论(0)
提交回复
热议问题