我使用此代码列出目录中的文件:
void ListDir(File f){
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
fileList.add(file.getPath());
}
ArrayAdapter<String> directoryList
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}
如何通过点击删除文件?我搜索了onListItemClick
。要删除文件,我需要:
File file = new File(uri.getPath());
file.delete();
如何将ArrayList
传递给uri
?
答案 0 :(得分:0)
为列表视图注册项目单击侦听器:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
File file = new File(fileList.get(position));
if(file.exists()){
file.delete();
}
}
});