ganjaam
ganjaam

Reputation: 1286

Can not load images with glide in recyclerview (data for recyclerview is fethced using retrofit)

My target is to fetch multiple image urls using an API and load those images in a 2d recyclerview. I'm using retrofit for fetching data and Glide for displaying the images in the recyclerview.

Apparantly, there is no issue with fetching the urls. But no image is being loaded in the recyclerview and it isn't displaying any exception or warning either.

Here's how I've initialized the recyclerview: (Class: HdrPhotoFragment.java)

        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(
                new GridLayoutManager(getContext(), SPAN_COUNT)
        );

Here's what I've done in onResponse() for fetching the model and setting the adapter for recyclerview: (Class: HdrPhotoFragment.java)

            @Override
            public void onResponse(Call<List<HdrPhotoListItemModel>> call, Response<List<HdrPhotoListItemModel>> response) {
                Log.d(TAG, "onResponse: " + response.code());
                if (response.code() != 200) {
                    Log.d(TAG, "onResponse: error fetching image data. CODE: ");
                    Toast.makeText(getContext(), "Error loading images", Toast.LENGTH_SHORT).show();
                    return;
                }
                mHdrPhotoModelList = response.body();
//                Log.d(TAG, "onResponse: response body" + response.body().toString());
                for (HdrPhotoListItemModel model : mHdrPhotoModelList) {
                    Log.d(TAG, "onResponse: Model = " + model.toString());
                }
                HdrPhotoListItemAdapter adapter = new HdrPhotoListItemAdapter(mHdrPhotoModelList, HdrPhotoFragment.this);
                mRecyclerView.setAdapter(adapter);
            }

And this is my onBindViewHolder() method where I've tried to load the image using glide: (Class: HdrPhotoListItemAdapter.java)

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HdrPhotoListItemModel itemModel = mHdrPhotoList.get(position);
//        holder.imageView.setImageResource(itemModel.getImageUrl());
        String url = Constants.BASE_URL + itemModel.getImageUrl();
        Log.d(TAG, "onBindViewHolder: URL: " + url);
        Glide.with(holder.imageView.getContext())
                .load(url)
                .into(holder.imageView);
    }

It is to be noted that the url obtained in onBindViewHolder() is accurate.

Upvotes: 0

Views: 1973

Answers (2)

ganjaam
ganjaam

Reputation: 1286

I'm not sure exactly for what reason the image wasn't loading. Using Picasso library instead of Glide solved this issue.

Inside onBindViewHolder() method, I just replaced

        Glide.with(holder.imageView.getContext())
                .load(url)
                .into(holder.imageView);

whith the following line:

        Picasso.get().load(url).into(holder.imageView);

Although it works, I would appreciate if anyone can tell me why it didn't work with Glide. My guess is that it has something to do with context.

Upvotes: 0

Greeshma
Greeshma

Reputation: 412

this should work (code snippet in Kotlin)

Glide.with(holder.itemView.context).load(url).into(holder.itemView.findViewById(R.id.image))

check the url also once

Upvotes: 1

Related Questions