java – RecyclerView only shows one image rather than multiple-ThrowExceptions

Exception or error:

I am trying to use RecyclerView to show a text and image.When i run my app in emulator it only shows one image that is pic2 and not pic one. I want both of them to be seen in my app when i run it on emulator.
It shows no error in my code when i add resource for pic 1 it still shows pic2. But when i make some changes in text it does show that change in emulator.

MainActivity.java

    import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //private LinearLayoutManager linearLayoutManager;
    //private List<ModelClass> item_list;
    //private modelAdapter mModelAdapter;

    private List<ModelClass2> item_list2;
    private modelAdapter2 mModelAdapter2;
    private LinearLayoutManager linearLayoutManager2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        item_list2 = new ArrayList<>();
        mModelAdapter2 = new modelAdapter2(this, item_list2);

        linearLayoutManager2 = new LinearLayoutManager(this);
        recyclerView.setAdapter(mModelAdapter2);
        recyclerView.setLayoutManager(linearLayoutManager2);


        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Shiv", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Andrew", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));

    }

}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

ModelClass2.java

 public class ModelClass2 {
    String Name;
    int imgresource;

    public ModelClass2(String name, int imgresource) {
        Name = name;
        this.imgresource = imgresource;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getImgresource() {
        return imgresource;
    }

    public void setImgresource(int imgresource) {
        this.imgresource = imgresource;
    }
}

ModelAdapter2.java

    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class modelAdapter2 extends RecyclerView.Adapter<modelAdapter2.MyViewHolder> {
    private Context context;
    private List<ModelClass2> itemlist2;

    public modelAdapter2(Context context, List<ModelClass2> itemlist2) {
        this.context = context;
        this.itemlist2 = itemlist2;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        ModelClass2 modelClass2=itemlist2.get(position);


        holder.textView.setText(itemlist2.get(position).getName());
        holder.imageView.setImageResource(R.drawable.pic2);

    }

    @Override
    public int getItemCount() {
        return itemlist2.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.t1);
            imageView = itemView.findViewById(R.id.i1);
        }
    }
}

recycler_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="8dp"
    android:background="#D74A66F1">

    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:height="70dp"
        android:fontFamily="casual"
        android:gravity="center"
        android:text="TextView"
        android:textColor="#3D3636"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/i1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_weight="2"
        android:scaleType="fitXY"
        app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
How to solve:

I think the problem is present inside the ModelAdapter2.java as in onBindViewHolder, you are setting image explicitly to R.drawable.pic2

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        ModelClass2 modelClass2=itemlist2.get(position);


        holder.textView.setText(itemlist2.get(position).getName());
        holder.imageView.setImageResource(R.drawable.pic2); <-----------

    }

I think it should be as follow:

holder.imageView.setImageResource(itemlist2.get(position).getImgresource());

Leave a Reply

Your email address will not be published. Required fields are marked *