示出在一个ListView文件的列表出在、文件、列表、ListView

由网友(月亮好像心动了)分享简介:我不知道如何从一个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);...

我不知道如何从一个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();

和通过 ArrayAdapter 添加到的ListView ,但我不明白的 ArrayAdapter 。

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.

阅读全文

相关推荐

最新文章