NeuralNotwork
NeuralNotwork

Reputation: 1

Glide isn't loading images from URL despite both placeholder image and URL in logs working

I'm trying to load images into a RecyclerView object via the Glide library but no matter what I change I can't get the image to successfully load.

'holder.image' (shown in the code snippet below) directly inserts the image into the RecyclerView ViewHolder and it successfully displays placeholder images if I include them. In addition the log snippet displays the URL link I am passing in, and when clicked the image is successfully displayed.

I have tried multiple different versions of Glide: 4.9, 4.4 and 3.7 (the most recent, the version in the tutorial I'm following and one recommended on a previous stack exchange answer).

I have included both the following permissions in the manifest: As well as just including one and including neither.

I have tried both HTTPS and HTTP links as well as links from different sites. (Including copying links directly from the tutorial I followed).

The below is my RecyclerViewAdapter class with all non-image variables removed. As far as I know the only key section is in the onBindViewHolder function as the placeholder is correctly working, but I have included the full (reduced) class just in case.

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private ArrayList<String> mImageAddresses = new ArrayList<>();

    public RecyclerViewAdapter(Context mContext, ArrayList<String> mImageAddresses) {
        this.mImageAddresses = mImageAddresses;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: called. Address name: " + mImageAddresses.get(position));
        Glide.with(mContext)
            .load(mImageAddresses.get(position))
            .placeholder(R.drawable.ic_launcher_foreground)
            .into(holder.image);

        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: clicked on: " + mTitles.get(position));
                Toast.makeText(mContext, "PLACEHOLDER", Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    public int getItemCount() {
        return mTitles.size(); //I have removed mTitles elsewhere for a more concise code snippet but it is successfully implemented
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.Icon);
        }
    }
}
2019-08-16 20:18:46.515 19359-19359/com.example.myapplication D/RecyclerViewAdapter: onBindViewHolder: called. Address name: http://c1.staticflickr.com/5/4636/25316407448_de5fbf183d_o.jpg

2019-08-16 20:18:46.745 19359-19359/com.example.myapplication D/RecyclerViewAdapter: onBindViewHolder: called. Address name: https://i.redd.it/obx4zydshg601.jpg

2019-08-16 20:18:46.764 19359-19359/com.example.myapplication D/RecyclerViewAdapter: onBindViewHolder: called. Address name: https://i.imgur.com/ZcLLrkY.jpg

The above are some sample log outputs using images from the tutorial I followed. All links are clickable. No errors seem to be appearing in the logs.

Upvotes: 0

Views: 48

Answers (1)

cutiko
cutiko

Reputation: 10517

A common problem is the position been final

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Glide.with(mContext)
        .load(mImageAddresses.get(holder.getAdapterPosition()))
        .placeholder(R.drawable.ic_launcher_foreground)
        .into(holder.image);
}

Upvotes: 0

Related Questions