Skip to content Skip to sidebar Skip to footer

If Recyclerview Scrolls, Then Item Id Changes. How To Resolve This?

This is my PDFListAdapter class method. I have downloaded file locally and save on Sqlite database. But if I scroll my RecyclerView, then I am having different item id. If not scro

Solution 1:

I have modified your onBindViewHolder function. The changes are following.

  1. Declared the pdfList variable as final and used it everywhere. It is not necessary to get the pdfList from the pdfModelClasses, each time you use it.
  2. The holder variable is removed, as it is not actually necessary. The holder1 is used everywhere.
  3. There are some changes with the visibility of the buttons. Check holder1.imgDownloadNote.setOnClickListener.

I have found no other problem in your onBindViewHolder. Please let me know if the modified code works.

@OverridepublicvoidonBindViewHolder(final MyViewHolder holder1, finalint index) {
    finalintposition= index;

    // Declare the variable here and make it final. finalPDFListpdfList= pdfModelClasses.get(position);

    finalDownloadedNotesDataBasedatabaseNotes=newDownloadedNotesDataBase(context);

    holder1.txtBookName.setText(pdfList.getSubjectName().toUpperCase());
    holder1.txtBookTitle.setText(StringUtils.getTrimString(pdfList.getTypeName()));
    holder1.txtBookBookDateOFIssue.setText(pdfList.getType());
    holder1.txtBookCategory.setText(StringUtils.getTrimString(pdfList.getDescription()));

    if (databaseNotes.isPurchasedNoteSaved(pdfList.getId(), final_nav_opt_name)) {
        holder1.txtDownload.setVisibility(View.VISIBLE);
        holder1.layout_download_note_option.setVisibility(View.GONE);
    } else {
        holder1.txtDownload.setVisibility(View.GONE);
        holder1.layout_download_note_option.setVisibility(View.VISIBLE);
    }

    holder1.layout_open_pdf.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            // I have the desired pdfList already, no need to get that again.// pdfList = pdfModelClasses.get(position);// Assigning to holder is not necessary.// holder = holder1;

            Log.e("PDFListAdapter", "layout_open_pdf position = "+position);
            Log.e("PDFListAdapter", "layout_open_pdf = "+pdfList.getId());
            if (databaseNotes.isPurchasedNoteSaved(pdfList.getId(), final_nav_opt_name)) {
                DownloadeNotesModeldownloadeNotesModel= databaseNotes.getNotesByID(pdfList.getId(), final_nav_opt_name);
                Intentintent=newIntent(context, PDFResults.class);
                intent.putExtra("pdfList", downloadeNotesModel.getFileLocation());
                intent.putExtra("from", "database");
                intent.putExtra("getSubjectName", downloadeNotesModel.getSubjectName());
                context.startActivity(intent);
            } else {
                final AlertDialog.BuilderalertDialog=newAlertDialog.Builder(context);
                alertDialog.setTitle("Alert");
                alertDialog.setCancelable(true);
                alertDialog.setMessage("Notes not downloaded. Do you want to download it?");
                alertDialog.setPositiveButton("Yes", newDialogInterface.OnClickListener() {
                    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)publicvoidonClick(DialogInterface dialog, int which) {
                        downloader = newDownloader();
                        newCheckSpace().execute(pdfList.getFileName());
                    }
                });

                alertDialog.show();
            }
        }
    });

    holder1.imgDownloadNote.setOnClickListener(newView.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)@OverridepublicvoidonClick(View v) {
            Log.e("PDFListAdapter", "imgDownloadNote position = "+position);
            Log.e("PDFListAdapter", "imgDownloadNote = "+pdfList.getId());
            pdfList = pdfModelClasses.get(position);
            deepak =index ;

            // We don't need the reassignment.// holder = holder1;if (!databaseNotes.isPurchasedNoteSaved(pdfList.getId(), final_nav_opt_name)) {
                if (UtilsMethods.isNetworkAvailable(context)) {
                    intresult= ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
                    if (result == PackageManager.PERMISSION_GRANTED) {
                        downloader = newDownloader();
                        newCheckSpace().execute(pdfList.getFileName());

                        // Make them visible when the internet connection is there. 
                        holder1.imgDownloadNote.setVisibility(View.VISIBLE);
                        holder1.imgCancelDownloadNote.setVisibility(View.VISIBLE);
                        holder1.progress_download_note.setVisibility(View.VISIBLE);
                    } else {
                        Toast.makeText(context, "storage permission is not granted", Toast.LENGTH_SHORT).show();
                        PermissionCheck.checkWritePermission(context);
                        holder1.imgDownloadNote.setVisibility(View.GONE);
                        holder1.imgCancelDownloadNote.setVisibility(View.GONE);
                        holder1.progress_download_note.setVisibility(View.GONE);
                    }
                } else {
                    holder1.imgDownloadNote.setVisibility(View.GONE);
                    holder1.imgCancelDownloadNote.setVisibility(View.GONE);
                    holder1.progress_download_note.setVisibility(View.GONE);
                    context.startActivity(newIntent(context, NoInternetActivity.class));
                }
            } else { 
                // Put proper visibility everywhere. 
                holder1.imgDownloadNote.setVisibility(View.GONE);
                holder1.imgCancelDownloadNote.setVisibility(View.GONE);
                holder1.progress_download_note.setVisibility(View.GONE);
                Log.e("","Not in db");
            }
        }
    });

    holder1.imgCancelDownloadNote.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            Log.e("PDFListAdapter", "imgCancelDownloadNote position = "+position);
            Log.e("PDFListAdapter", "imgCancelDownloadNote = "+pdfList.getId());
            finalAlertDialogalertDialog=newAlertDialog.Builder(context, R.style.AlertDialogStyle).create();
            alertDialog.setTitle("Alert");
            alertDialog.setMessage("Are you sure want to cancel download?");
            alertDialog.setButton("Yes", newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int which) {
                    alertDialog.hide();
                    downloader.cancel(true);
                }
            });
            alertDialog.show();
        }
    });
}

@OverridepublicintgetItemCount() {
    return pdfModelClasses.size();
}

@OverridepublicintgetItemViewType(int position) {
    return position;
}

Post a Comment for "If Recyclerview Scrolls, Then Item Id Changes. How To Resolve This?"