Skip to content Skip to sidebar Skip to footer

Showing A List Of Files In A Listview

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 Li

Solution 1:

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 = newString[filelist.length];
for (int i = 0; i < theNamesOfFiles.length; i++) {
   theNamesOfFiles[i] = filelist[i].getName();
}

The adapter to use with the list :

newArrayAdapter<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.

Solution 2:

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

FiledataDirectory= Environment.getDataDirectory();
FilefileDir=newFile(dataDirectory, "data/com.yourapp.app/files");

String[] listItems = fileDir.list();
Arrays.sort(listItems);

Post a Comment for "Showing A List Of Files In A Listview"