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();
and added to the ListView
via ArrayAdapter
but I don't get the usage of the ArrayAdapter
.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With