I am new to Android. I am currently trying to build an app having a list view screen where a list of books is displayed. On each item, there is an image downloaded from URL and 2 icons who's image resource is set based on values set in a POJO object.
My Adapter code is as below:
Context context = view.getContext();
view.setLayoutManager(new LinearLayoutManager(context));
view.setAdapter(new MySearchBooksItemRecyclerViewAdapter(SearchBooks.ITEMS, mListener, getActivity()));
public void onBindViewHolder(final ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: binding variables of object: " + position);
Log.d(TAG, "onBindViewHolder: holder value: " + holder.mItem);
Log.d(TAG, "onBindViewHolder: holder value: " + holder.mTitle);
Log.d(TAG, "onBindViewHolder: holder value: " + holder.mISBN);
holder.mItem = mValues.get(position);
holder.mTitle.setText(mValues.get(position).getTitle());
holder.mTitle.setTag(mValues.get(position).getId());
holder.mISBN.setText(mValues.get(position).getIsbn());
holder.mDesc.setText(mValues.get(position).getDesc());
holder.mView.setOnClickListener(new ClickListItem());
getImages(holder.mBookImage, mValues.get(position).getImage());
if (bookList.get(mValues.get(position).getId()) != null) {
holder.mIsFav.setImageResource(bookList.get(mValues.get(position).getId()).isBookmarked() ? R.drawable.ic_favorite_white_24dp : R.drawable.ic_favorite_border_white_24dp);
holder.mIsDownloaded.setImageResource(new File(bookList.get(mValues.get(position).getId()).getSavedBookName()).exists() ? R.drawable.ic_offline_pin_white_24dp : R.drawable.ic_get_app_white_24dp);
}
Log.d(TAG, "onBindViewHolder: variables bound: " + mValues.get(position));
}
@Override
public int getItemCount() {
Log.d(TAG, "getItemCount: item count: " + mValues.size());
return mValues.size();
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/darker_gray"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:background="@color/background_material_dark"
android:orientation="vertical">
<ImageView
android:id="@+id/bookImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="100dp"
android:layout_weight="2"
android:src="@drawable/icon_img_tu" />
<LinearLayout
android:layout_weight="1"
android:minHeight="40dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/is_fav_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_favorite_border_white_24dp" />
<ImageView
android:id="@+id/is_downloaded_image"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_material_dark"
android:src="@drawable/ic_get_app_white_24dp" />
</LinearLayout>
</LinearLayout>
Now the problem comes when I scroll through the screen. in the first load the icon values and images are perfectly loaded. but when I scroll across and return back, the 'onBindViewHolder' is again called and wrong random values are used. thus showing wrong icons in the images.
can anyone help me how to fix this...??
i don't want the images to be reloaded or the icons to be refreshed unless needed
You should add else statement where you checking the id for null, in else clause populate static images or any place holders.
This happens when you did not load image in ImageView and ImageView get the random image. In your case you have only if statement so ImageView does not load any image when if statement has false. Recyclerview recycles the view but we have to make sure to populate all the values in recyclerview row otherwise previous images/data will be appear.
Hope this helps. If it solve your problem then accept it. Thanks