我需要使用目录中的文件填充listview
。我可以将文件放入数组中,但似乎无法将它们放入listview
。这是我的代码;我哪里错了?ListView未从数组中填充
private void getDir(final String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(String.valueOf(dirPath));
final File[] files = f.listFiles();
myList = (ListView)findViewById(android.R.id.list);
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
// For testing only ---
System.out.println("Files -- "+files[i]);
System.out.println("Files -- "+file);
// End testing ^^^^^
if (!file.isHidden() && file.canRead()) {
path.add(file.getPath());
if (file.isDirectory()) {
item.add(file.getName() + "/");
}
else {
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.activity_sendsave, android.R.id.list);
setListAdapter(fileList);
}
else {
Toast.makeText(this,"No Files",Toast.LENGTH_LONG).show();
}
}
这里是我的xml文件。
<ListView
android:id = "@android:id/list"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentTop = "true"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_above = "@+id/btnSendSave"
android:clickable = "true"
android:focusable = "true"
android:visibility = "visible" />
我需要让你知道,一切都似乎是在做什么应该,但listview
不会填满。
在代码中,有system.out.println(files[i])
显示文件正在放入数组中。
对不起,setListAdapter什么都不做。这是从我尝试的另一块代码中遗留下来的。而类是公共类GetFile扩展ListActivity,但我相信这与它无关。 – vbneil54