GridView,Picasso 没有加载到 ImageView

GridView, Picasso Not Loading Into ImageView

我无法使用 Picasso 将图像加载到 ImageView。我搜索了很多,但找不到答案。

有一个 GridView,GridView 有一个 ImageViewTextView。我使用 Picasso 将图像加载到 ImageView。

这是适配器:

public class MyGridAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
public List<Photo> data ;
Photo photo = new Photo();

public MyGridAdapter(Context context,
                     List<Photo> arraylist) {
    this.context = context;
    data = arraylist;
    //imageLoader = new ImageLoader(context);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView phototitle;
    ImageView imageView;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.gridview_item, parent, false);
    // Get the position
    photo = data.get(position);

    phototitle = (TextView) itemView.findViewById(R.id.phototitle);
    imageView = (ImageView) itemView.findViewById(R.id.imageView1);


    phototitle.setText(photo.getTitle());

    String url = photo.getUrl();
    Log.d("url", url);
    Picasso.with(context).setLoggingEnabled(true);
    Picasso.with(context).load(url).into(imageView);

    return itemView;
}
}

这是 GridView 项:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="200dp"
    android:layout_height="200dp" />

<TextView
    android:id="@+id/phototitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="10sp"/>

</LinearLayout>

输出:

 12-23 22:03:03.921 18840-18840/com.mehmetsefa.challenge D/url: http://placehold.it/150/bb7f4
12-23 22:03:03.926 18840-18840/com.mehmetsefa.challenge W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
12-23 22:03:03.931 18840-18840/com.mehmetsefa.challenge D/Picasso: Main        created      [R0] Request{http://placehold.it/150/bb7f4}
12-23 22:03:03.936 18840-19224/com.mehmetsefa.challenge D/Picasso: Dispatcher  enqueued     [R0]+2ms 
12-23 22:03:03.936 18840-19226/com.mehmetsefa.challenge D/Picasso: Hunter      executing    [R0]+2ms 
12-23 22:03:03.946 18840-18840/com.mehmetsefa.challenge D/url: http://placehold.it/150/bb7f4

我做错了什么?

你没有做错,只是你的图片有问题。我猜这是因为它没有扩展名,或者在请求发出后没有立即形成。您的图像是用某种 CG 库形成的,它会在请求完成后立即生成图像,因此正文返回为空。

您主要是缺少 INTERNET 权限。当您尝试调试 Picasso 时,我的建议是:

像这样创建一个生成器:

Picasso.Builder builder = new Picasso.Builder(context);
        builder.listener(new Picasso.Listener()
        {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
            {
                exception.printStackTrace();
            }
        });
        picasso = builder.build();

使用上面的实例加载图片:

picasso.load(url).into(imageView);

如果有任何错误,您将在日志中看到。祝你好运。