这是我浏览设备中所有文件夹的代码。
private File file;
private List<String> directoriesList;
private ListView listView ;
private ArrayAdapter<String> adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.folder_browse_screen);
listView = (ListView) findViewById(R.id.foldersListView);
directoriesList = new ArrayList<String>();
file = new File("/mnt");
File list[] = file.listFiles();
if (list != null && list.length >= 1) {
for (int i = 0; i < list.length; i++) {
File f = list[i];
if (!f.isHidden() && f.canRead() && f.isDirectory()) {
directoriesList.add(f.getName());
}
}
adapter = new ArrayAdapter<String>(this, R.layout.list_row, R.id.folder_name, directoriesList);
listView.setAdapter(adapter);
}
OnItemClick :
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
try {
File temp_file = new File(file, directoriesList.get(position));
if (!temp_file.isHidden() && temp_file.canRead() && temp_file.isDirectory()) {
file = new File(file, directoriesList.get(position));
File list[] = file.listFiles();
directoriesList.clear();
if (list != null && list.length >= 1) {
for (int i = 0; i < list.length; i++) {
File f = list[i];
if (!f.isHidden() && f.canRead() && f.isDirectory()) {
directoriesList.add(f.getName());
}
}
adapter.notifyDataSetChanged();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
异常:java.lang.IllegalStateException:适配器的内容已更改,但 ListView 未收到通知。确保适配器的内容不是从后台线程修改的,而只是从 UI 线程修改的。[在 ListView(2131034114, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
我在哪里使用线程?为什么单击任何空文件夹时强制关闭?请帮我。