androidimageandroid-studiopicasso

Unable to import images from Picasso


Please can anyone look into this code and help me identify why I'm unable to import images from Picasso:

TaskListAdapter.java:

package com.dummies.tasks.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.dummies.tasks.activity.R;
import com.squareup.picasso.Picasso;

public class TaskListAdapter
        extends RecyclerView.Adapter<TaskListAdapter.ViewHolder>
{
        static String[] fakeData = new String[] {
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Ah . . . ah . . . ah!"
    };

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        // create a new view
        CardView v = (CardView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_task, parent, false);
    // wrap it in a ViewHolder
        return new ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.titleView.setText(fakeData[i]);

        String url = "http://lorempixel.com/600/400/cats/?fakeId=";
        Picasso.get().load(url).into(viewHolder.imageView);

    }

    @Override
    public int getItemCount() {
        return fakeData.length;
    }

    static class ViewHolder extends RecyclerView.ViewHolder { 
        CardView cardView;
        TextView titleView;
        ImageView imageView;

        public ViewHolder(CardView card) {
            super(card);
            cardView = card;
            titleView = card.findViewById(R.id.text1);
            imageView = card.findViewById(R.id.image);
        }
    }
}

I also added the build.gradle dependency:

implementation 'com.squareup.picasso:picasso:2.71828'

and as well added internet permission:

<uses-permission android:name="android.permission.INTERNET"/>

The whole code is error-free except for the fact that it doesn't display images when I run the app.


Solution

  • I am a beginner having trouble with the same issue. I solved it by changing the method Michael Burton used on the bottom of page 179 and making it an if/else statement, pulling images from my own site because his reference was no longer available:

    public static String getImageUrlForTask(long taskId) {
        if (taskId == 0)            
            return "https://www.promisesandsecrets.com/images/BigBang.jpg";
        else if (taskId == 1)
            return "https://www.promisesandsecrets.com/images/broodofvipers.jpg";
        else if (taskId == 2)
            return "https://www.promisesandsecrets.com/images/man.jpg";
        else
            return "https://www.promisesandsecrets.com/images/church.jpg";
    }
    
    It's not beautiful, but it worked and helped me understand the code.