Take the 2-minute tour ×

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.

share|improve this question
what is exactly your question? there is plenty of SO questions about 'how to manage an arrayAdapter', 'how to create a listView'. For you I think you want to just display the name of different files from a folder, to a listview. You will have to pass an ArrayList of Strings to your adapter. Look at the official documentation there is some examples about how to do a ListView. –  Jeremy D Feb 16 '12 at 19:13

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
How about names of the file plus the date of creation/modified? –  SiKni8 Aug 2 '13 at 18:36
@SiKni8 The File class unfortunately exposes only the date of the last file modification and not the creation date. To show that besides the file's name you could use a custom adapter or a Hashmap containing the data and use a SimpleAdapter. –  Luksprog Aug 3 '13 at 6:48
Thanks for the response. I think Modified Date should suffice for me. One question I have is I want to display a custom icon followed by the filename top right of icon and modified date bottom right of icon. How would I achieve that? –  SiKni8 Aug 3 '13 at 12:12

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();
share|improve this answer

Your Answer


