简体   繁体   中英

How to import images from Picasso

I'm writing an app as a learner based on an android development book. In the book, the author wrote some certain codes that prove to be efficient but based on the fact that it was written in 2015, some features have changed on the recent android documentations.

My adapter.java code issued an error on the .into(imageView) part and if I click alt + enter, the suggestions they give me are: Create local variable 'imageView', and some others but none of those seem to solve the issue.

Adapter.java code below:

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]);

// set the thumbnail image
Picasso.get().load("http://lorempixel.com/600/400/cats/?fakeId=").into(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);
}
}
}

When I first ran the code without Picasso, it ran without error but importing from Picasso is the issue.

I also included <uses-permission android:name="android.permission.INTERNET"/> in android manifest.xml

The app should look like this when it's done: https://i.stack.imgur.com/LVSJZ.png

把 viewHolder.imageView 像那样 Picasso.get().load("http://lorempixel.com/600/400/cats/?fakeId=").into(viewHolder.imageView);

Try this one:

com.squareup.picasso:picasso:2.5.2

Picasso.with(context).load(image_url).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM