[英]Android java : How to auto refresh list view
我目前正在开发File Explorer应用程序,并且需要自动刷新列表视图。 因为当我删除项目时,我仍然可以看到该项目,所以我需要退回该文件夹并再次进入该文件夹,只看到它消失。 如何自动刷新,所以当我删除项目时它会自动消失。
@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();
}
从填充listview的数据源中删除项目,并在适配器上调用notifyDataSetChanged
以刷新listView
您需要从Adapter的ArrayList中删除ListItem并调用notifyDatasetChanged(),当您调用notifyDatasetChanged()方法时,它将使用具有更新数据的ArrayList刷新列表视图,因此,您必须首先从ArrayList onClick的删除按钮中删除项目。 然后,您必须调用notifyDatasetChanged()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.