1
我有很好的工作android列表,我只需要显示目录。 我尝试了一些方法,但它没有做事。 使用isDirectory()方法,我想我不是那样使用它。如何获取唯一的目录列表android
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
path = Environment.getExternalStorageDirectory().toString();
if (getIntent().hasExtra("path")) {
path = getIntent().getStringExtra("path");
}
// Read all files sorted into the values-array
List<String> values = new ArrayList();
File dir = new File(path);
if (!dir.canRead()) {
setTitle(getTitle() + " (inaccessible)");
}
String[] list = {};;
if (new File(path).isDirectory() == true) {
list = dir.list();
} else {return;}
if (list != null) {
for (String file : list) {
if (new File(path).isDirectory() == true) { // !file.startsWith(".") &&
values.add(file);
} else {return;}
}
}
Collections.sort(values);
// Put the data into the list
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_2, android.R.id.text1, values);
setListAdapter(adapter);
}
是啊,我尝试这样做,我不'吨明白为什么它不'吨我的代码工作。 – Piarix 2014-09-05 11:24:33
list = dir.list();使用listFiles()方法代替 – deniz 2014-09-05 11:35:06
请调试您的应用程序并找到问题:) – deniz 2014-09-05 11:41:47