Skip to content
Advertisement

Recyclerview is not dispaying the image

I am using this code to display a preset image in recyclerview, but for some reason it wont display. If anyone has any fixes I would be greatly indebted to them if they share it.

My Main Activty:

public class MangadexDisplay extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mangadex_display);
        ArrayList<String> imgUrl = new ArrayList<>();


        RecyclerView recyclerView = findViewById(R.id.mangaRecycler);

        LinearLayoutManager Manager = new LinearLayoutManager(MangadexDisplay.this);
        recyclerView.setLayoutManager(Manager);
        MyAdapter adapter = new MyAdapter(imgUrl, MangadexDisplay.this);
        recyclerView.setAdapter(adapter);

        imgUrl.add("http://thewowstyle.com/wp-content/uploads/2015/01/nature-images..jpg");
        adapter.notifyDataSetChanged();


    }


}

My MyAdapter.java:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

    ArrayList<String> urls;
    Context context;
    //constructor
    public MyAdapter(ArrayList<String> ImgUrl, Context context_)
    {
        this.urls = ImgUrl;
        this.context = context_;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        private final ImageView image;

        public ViewHolder(View v)
        {
            super(v);
            image =(ImageView)v.findViewById(R.id.mangaImage);
        }

        public ImageView getImage(){ return this.image;}
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_mangadex_display, parent, false);
        v.setLayoutParams(new RecyclerView.LayoutParams(1080,800));
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position)
    {
        Glide.with(this.context)
                .load(urls.get(position))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.getImage());
    }

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

}

If it helps I am using glide as shown above to load the image in, If there is another alternative should this be the issue I would be glad to hear it.

Here is the Layout code:

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MangadexDisplay"
    android:orientation="vertical">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/mangaRecycler"
        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" />

    <ImageView
        android:id="@+id/mangaImage"
        android:layout_width="381dp"
        android:layout_height="650dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

Advertisement

Answer

You need an seperate item for the image. e.g. create a file in layouts item_manga.xml with

<ImageView
    android:id="@+id/mangaImage"
    android:layout_width="381dp"
    android:layout_height="650dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

And your viewholder should look somehow like this

 @Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
    View itemView = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.item_manga, viewGroup, false);
    return new ViewHolder(itemView);
}

I have no experience with Glide, so I cannot say anything about it. But you can try it with a local image to see if it works

holder.image.setImageBitmap(bitmap)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement