我正在尝试获取文件夹中的文件列表及其扩展名为 ".mp3" 的子文件夹,但该函数仅在根文件夹中返回 .mp3 文件。
我需要一个看起来像这样的结构
http://s1.ipicture.ru/uploads/20130530/Kiju7Bf7.png 源代码:
private ListView lvFolders;
private File file;
private List<String> myList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.folder, null);
lvFolders = (ListView) v.findViewById(R.id.lvFolders);
myList = new ArrayList<String>();
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File(root_sd);
addToSongs(file);
/*File list[] = file.listFiles();
for (int i = 0; i < list.length; i++) {
myList.add(list[i].getName());
}*/
lvFolders.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, myList));
lvFolders.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
File temp_file = new File(file, myList.get(position));
if (!temp_file.isFile()) {
file = new File(file, myList.get(position));
File list[] = file.listFiles();
myList.clear();
for (int i = 0; i < list.length; i++) {
myList.add(list[i].getName());
}
Toast.makeText(getActivity(), file.toString(),
Toast.LENGTH_LONG).show();
lvFolders.setAdapter(new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,
myList));
}
}
});
return v;
}
FilenameFilter textFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3"));
}
};
private void addToSongs(File home) {
for (File file : home.listFiles(textFilter)) {
if (file.isDirectory()) {
addToSongs(file);
} else {
myList.add(file.getAbsolutePath());
}
}
}
我做错了什么?