示出在一个ListView文件的列表 [英] Showing a list of files in a ListView

查看:109
本文介绍了示出在一个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);
File[] filelist = dir.listFiles();

和通过 ArrayAdapter 添加到的ListView ,但我不明白的<$ C $的用法C> 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.

这篇关于示出在一个ListView文件的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆