Java 如果用户已从存储目录中删除了项目表单recyclerview,如何删除它?
下面是我尝试的代码Java 如果用户已从存储目录中删除了项目表单recyclerview,如何删除它?,java,android,android-recyclerview,Java,Android,Android Recyclerview,下面是我尝试的代码 import android.os.FileObserver; import android.support.annotation.Nullable; import android.util.Log; public class ObserveFiles extends FileObserver { public RecyclerViewAdapter recyclerViewAdapter; public String absolutePath; public Obse
import android.os.FileObserver;
import android.support.annotation.Nullable;
import android.util.Log;
public class ObserveFiles extends FileObserver {
public RecyclerViewAdapter recyclerViewAdapter;
public String absolutePath;
public ObserveFiles(String path) {
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
}
@Override
public void onEvent(int event, @Nullable String path) {
if (path == null) {
return;
}
//a new file or subdirectory was created under the monitored directory
if ((FileObserver.DELETE & event)!=0) {
Log.d("Deleted---------->", "File Deleted [" + absolutePath + "/" + path + "]");
String filepath = absolutePath + "/" + path;
recyclerViewAdapter.removeOutOfApp(filepath);
}
//data was written to a file
if ((FileObserver.MODIFY & event)!=0) {
Log.d("Deleted---------->", "File Modified [" + absolutePath + "/" + path + "]");
}
//the monitored file or directory was deleted, monitoring effectively stops
if ((FileObserver.DELETE_SELF & event)!=0) {
Log.d("Deleted---------->", "File Self Deleted [" + absolutePath + "/URecorder" + "/" + path + "]");
}
//a file or directory was opened
if ((FileObserver.MOVED_TO & event)!=0) {
Log.d("Deleted---------->", "File Moved To [" + absolutePath + "/" + path + "]");
}
//a file or subdirectory was moved from the monitored directory
if ((FileObserver.MOVED_FROM & event)!=0) {
Log.d("Deleted---------->", "File Moved From [" + absolutePath + "/" + path + "]");
}
//the monitored file or directory was moved; monitoring continues
if ((FileObserver.MOVE_SELF & event)!=0) {
Log.d("Deleted---------->", "File Moved Self[" + absolutePath + "/" + path + "]");
}
}
}
@Override
protected void onResume() {
super.onResume();
String filePath = android.os.Environment.getExternalStorageDirectory().toString() + "/URecorder";
observeFiles = new ObserveFiles(filePath);
observeFiles.startWatching();
}
@Override
protected void onDestroy() {
super.onDestroy();
observeFiles.stopWatching();
}
欢迎来到SO!:)
以下是伪代码:
1) 删除:从RecyclerView中删除项目有4个步骤
list.remove(position);
recycler.removeViewAt(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, list.size());
这些代码行对我有用
2) 更新数据:我唯一要做的就是
mAdapter.notifyDataSetChanged();
所有这些都必须在Activity/Fragment代码中完成,而不是在RecyclerView适配器代码中完成
希望能有所帮助。我不知道你的数据/文件数量,也不知道你在做什么任务。 但另一种可选方法是在
OnResume()中加载数据代码>。
如果用户
最小化应用程序>删除文件..>当应用程序出现在前台时,将再次调用onResume。
因此,每次它都会加载新数据,除非其他背景内容会删除该文件 String path=“file://”+newfile[i].getAbsolutePath()
文件=新文件(路径)
delete()
这是直接从存储中删除文件的过程如果用户从外部/内部存储目录中删除文件,我希望从回收器视图和sqlite中删除。否如果用户从存储目录中删除文件,我希望更新回收器视图