Search code examples
androidandroid-recyclerviewdownloadfirebaseuiandroid-viewholder

How to get correct RecyclerView ViewHolder after leaving the activity


I have a recyclerview and I populate data on it using firebaserecycleradapter. Every item in the list contains reference to firebase storage. So When I click on download button (red button), the download start and I can get informations about the view. When the download is in progress and I leave the activity and come back the download still on progress but never return the correct item View.

How can I get the correct item View when I come back so that I can show the download progress at specific item and make some changes to that specific item when download complete.

Recyclerview items and download button in red

Code behind download button

storageReference.getFile(zipFile)
                    .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            Log.d("visibilitedocSuccess", ""+holder+"|"+key+"|holder"+holder+"|holderid:"+holder.getAdapterPosition()+"|oldpos:"+holder.getOldPosition());

                                holder.txtProgress.setVisibility(View.GONE);
                                holder.progressBar.setVisibility(View.GONE);
                                holder.imgDocumentChecker.setVisibility(View.VISIBLE);
                                holder.imgDocumentChecker.setImageResource(R.drawable.done_ic);

                        }
                    })
                    .addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                            Log.d("visibilitedocProgress:", progress + "% done"+"|holder"+holder+"|holderid:"+holder.getAdapterPosition()+"|oldpos:"+holder.getOldPosition());
                            holder.txtProgress.setText(((int) progress) + "%");
                            holder.progressBar.setProgress((int) progress);

                        }
                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d("visibilitedocFail:", e + "");
                    holder.txtProgress.setVisibility(View.GONE);
                    holder.progressBar.setVisibility(View.GONE);
                    holder.imgDocumentChecker.setVisibility(View.VISIBLE);
                    holder.imgDocumentChecker.setImageResource(R.drawable.download_icon);
                    Toasty.error(DocumentsListActivity.this, "Chargement annulé", Toast.LENGTH_LONG, true).show();
                }
            });

Logcat shows below informations related to the ViewHolder

ViewHolder{ace0130 position=-1 id=-1, oldPos=-1, pLpos:-1 unbound no parent}

How to fixe it please ?


Solution

  • I am not entirely sure by what you mean by "get the correct item View". However what I understand from your question I think the issue you are having is coming from the fact that you do not update the UI.

    Let me explain, whenever you get out and comeback to the activity which is downloading you "recreate" the RecyclerView. So You cannot access the previous items that where displayed.

    Two choices here:

    • Either you save the state of the things you are downloading (you can find lots of articles on that using google :))
    • Or you have some class in your code that keeps the information that way when you go back to the downloading activity you simply need to update the view items :)