gpt4 book ai didi

java - 安卓java : How to auto refresh list view

转载 作者:行者123 更新时间:2023-12-01 13:16:19 27 4
gpt4 key购买 nike

我目前正在开发一个文件资源管理器应用程序,我需要为我的 ListView 自动刷新。因为当我删除该项目时,我仍然可以看到该项目,我需要返回该文件夹并再次进入该文件夹,然后才能看到它消失。如何自动刷新,以便当我删除该项目时它会自行消失。

@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

关于java - 安卓java : How to auto refresh list view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22439528/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com