在 ListView 中显示文件列表 [英] Showing a list of files in a ListView

查看:31
本文介绍了在 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 但我没有得到 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天全站免登陆