我从适配器(Myadpter
)内部有(删除文件操作),删除文件后我无法刷新此适配器,只是删除了文件,屏幕将保持不变,但我需要刷新状态手动按一下,然后重新进入相同的布局,这样我会发现该布局缺少我删除的文件,但是需要退回并重新进入相同的布局才能刷新它。
有人知道为什么notifyDataSetChanged();
无法正常工作吗?
package com.example.boc.storage;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.boc.R;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.storage.FirebaseStorage;
import java.util.ArrayList;
import java.util.List;
import static android.os.Environment.DIRECTORY_DOWNLOADS;
import static com.example.boc.Pdf.pdfDownloader.TAG;
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
public MainActivity mainActivity;
public ArrayList<DownModel> downModels;
RecyclerView mRecyclerView;
ArrayList<DownModel> downModelArrayList = new ArrayList<>();
public MyAdapter (MainActivity mainActivity, ArrayList<DownModel> downModels) {
this.mainActivity = mainActivity;
this.downModels = downModels;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(mainActivity.getBaseContext());
View view = layoutInflater.inflate( R.layout.storage_elements, null, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
myViewHolder.mName.setText( downModels.get( i ).getName() );
myViewHolder.deletePdfBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = myViewHolder.mName.getContext();
if (context != null) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
String filenamed = (String) myViewHolder.mName.getText();
String url = downModels.get( i ).getLink() ;
firebaseStorage.getReferenceFromUrl( url ).delete()
.addOnSuccessListener( new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d( TAG, "DocumentSnapshot successfully written!" );
}
} );
db.collection( "Storage" ).document( filenamed ).delete()
.addOnSuccessListener( new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d( TAG, "DocumentSnapshot successfully written!" );
notifyDataSetChanged();// this method never helped //
}
} )
.addOnFailureListener( new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w( TAG, "Error writing document", e );
}
} )
;
} else {
return;
}
}
} );
myViewHolder.mDownload.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadFile( myViewHolder.mName.getContext(), downModels.get( i ).getName(), ".pdf", DIRECTORY_DOWNLOADS, downModels.get( i ).getLink() );
}
} );
}
public void downloadFile(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {
DownloadManager downloadmanager = (DownloadManager) context.
getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);
downloadmanager.enqueue(request);
}
@Override
public int getItemCount() {
return downModels.size();
}
}
答案 0 :(得分:1)
在成功删除之后,您还需要删除该DownModel,然后调用notifyDataSetChanged();
,这样:
public void onSuccess(Void aVoid) {
Log.d( TAG, "DocumentSnapshot successfully written!" );
downModels.remove(i);
notifyDataSetChanged();
}
答案 1 :(得分:1)
尝试一下!
public void onSuccess(Void aVoid) {
Log.d( TAG, "DocumentSnapshot successfully written!" );
notifyItemRangeChanged(i, downModels.size());
downModels.remove(i);
notifyItemRemoved(i);
}