'Icons not showing in Image View

I am trying to show the items into Recycler View in Grid Layout Manager. My list item xml file is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView

    app:cardElevation="3dp"
    app:cardUseCompatPadding="true"
    app:cardCornerRadius="10dp"
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/cardView">

    <LinearLayout
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/itemLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="10dp"
            android:adjustViewBounds="true"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/health_2" />

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:ellipsize="end"
            android:ems="5"
            android:gravity="center"
            android:lines="2"
            android:maxEms="5"
            android:maxLines="2"
            android:text="Health" />

    </LinearLayout>

</android.support.v7.widget.CardView>

</RelativeLayout>

And my code snippet is :

MainActivity :

    mLayoutManager = new GridLayoutManager(MainActivity.this, 3);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(mLayoutManager);
    adapter = new MyAdapter (MainActivity.this, 
    arrayList);
    recyclerView.setAdapter(adapter);

MyAdapter :

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {

    model = arraylist.get(position);
    holder.itemIcon.setImageResource(model.getIconName());

}

I am setting the icon name to model for the list in this way :

  model.setIconName(R.mipmap.health_2);

Model Class :

public class DashBoardModel {

String itemName;
int iconName;

public String getItemName() {
    return itemName;
}

public void setItemName(String itemName) {
    this.itemName = itemName;
}

public int getIconName() {
    return iconName;
}

public void setIconName(int iconName) {
    this.iconName = iconName;
}
}

My question is after doing these things , i am getting the output list as shown in the image below which is not the expected icon to be seen, How to resolve this ?

enter image description here



Solution 1:[1]

you could use the glide library com.github.bumptech.glide:glide:+ to dynamically load the icons into the image view, like so:

//assuming the getIconName method return an int, i.e R.mipmap.whatever
Glide.with(mContext).load(model.getIconName()).into(holder.itemIcon);

Solution 2:[2]

I have this same issue. Although I don't know why setting the image from the adapter doesn't work, I resolved it by posting a callback on ImageView's Handler.

@Override
fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    holder.itemIcon.post {
        holder.itemIcon.setImageResource(...)
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maverick Paschal
Solution 2 ashu