我在从RecyclerView中删除位置(主要是图像)方面遇到了一些问题。我将图像存储在应用程序的内存中,并在DB中为它们提供引用(作为路径)。
此外,我使用滑动将图像从内存加载到图像视图中。我试图删除图片:
信息(MyFunnyImg对象列表) 一切看起来都很好 - 图像从列表中消失但是在重新输入这个片段(包含recyclerView)后,图像突然出现......
我试图在互联网上找到一些东西,但没有任何效果,这是我的代码(处于'开发'状态):
从ContentProvider中删除方法:
@Override
public int delete(@NonNull Uri uri, @Nullable String where, @Nullable String[] whereArgs) {
int numbersOfRowsDeleted=0;
switch (uriMatcher.match(uri)) {
case ONE_PIC:
numbersOfRowsDeleted=dbHelper.getWritableDatabase().delete(DatabaseDescription.Picture.TABLE_NAME_,
DatabaseDescription.Picture._ID+"="+uri.getLastPathSegment(),whereArgs);
break;
default:
unsupportedOperation();
}
if(numbersOfRowsDeleted>0){
getContext().getContentResolver().notifyChange(uri,null);
}
return numbersOfRowsDeleted;
}
来自RecyclerViewAdapter的方法,用于从DB中删除图像和行
private boolean onSingleMenuItemClick(MenuItem item,ImageView imageView,int position){
int id=item.getItemId();
switch (id){
case R.id.share:
// instructions
break;
case R.id.more_info:
// instructions
break;
case R.id.delete_img:
deleteImg(position);
break;
default:
//instructions
}
return true;
}
private void deleteImg(int position){
removeImgFile(argsToShow.get(position).getFileName());
deleteFromDatabase(position);
argsToShow.remove(position);
//notifyDataSetChanged();
// notifyItemRemoved(position);
notifyItemRangeChanged(position,argsToShow.size());
}`private void removeImgFile(String fName){
String root=null;
try {
root = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString();
}catch(NullPointerException ex){
ex.printStackTrace();
Log.e("Error","NullPointerException while deleting BitMap");
}
File dir=new File(root+"/saved_images");
File file=new File(dir,fName);
}
private void deleteFromDatabase(int position){
// we create URI referrimng to whole table
Uri uri= DatabaseDescription.Picture.CONTENT_URI;
String id=String.valueOf(position);
uri=uri.buildUpon().appendPath(id).build();
ContentResolver contentResolver=activity.getContentResolver();
int count= contentResolver.delete(uri,null,null);
ViewUtils.showToast(activity,"Deleted: "+count+" rows");
}