我目前正在开发一个文件资源管理器应用程序,我需要为我的列表视图自动刷新。 因为当我删除项目时,我仍然可以看到该项目,我需要备份文件夹并再次进入文件夹,只看到它消失。如何自动刷新所以当我删除项目时它会消失。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
FileInfo fileDescriptor = fileArrayListAdapter.getItem(position);
if (fileDescriptor.isFolder() || fileDescriptor.isParent()) {
currentFolder = new File(fileDescriptor.getPath());
fill(currentFolder);
//
} else {
fileSelected = new File(fileDescriptor.getPath());
Intent intent = new Intent();
intent.putExtra(Constants.KEY_FILE_SELECTED,
fileSelected.getAbsolutePath());
setResult(Activity.RESULT_OK, intent);
Log.i("FILE CHOOSER", "result ok");
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(fileSelected.getName());
String type = map.getMimeTypeFromExtension(ext);
if (type == null){
type = "*/.jpeg*";
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(fileSelected), "image/*");
// intent.setDataAndType(data, type);
}else {
type = "*/.txt*";
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(fileSelected), "text/*");
// intent.setDataAndType(data, type);
}
//finish();
// intent.setAction(android.content.Intent.ACTION_VIEW);
startActivity(intent);
// Intent intent = new Intent();
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
new AlertDialog.Builder(ViewNoteActivity.this) .setTitle("Delete File")
.setMessage("Do You Want To Delete this file ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int button){
fileSelected.delete();
Toast.makeText(getBaseContext(), "File has been deleted." , Toast.LENGTH_SHORT ).show();
adapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int button){
}
}).show();
return true;
}
});
}
}
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
答案 0 :(得分:1)
从填充listview的数据源中删除项目,并在适配器上调用notifyDataSetChanged
以刷新listView
答案 1 :(得分:0)
您需要从Adapter中删除ArrayList中的ListItem并调用notifyDatasetChanged(), 当你调用notifyDatasetChanged()方法时,它将使用带有更新数据的ArrayList刷新listview,因此,你必须首先从删除按钮上的ArrayList中删除项目。 然后你必须调用notifyDatasetChanged()。