我无法使用ImageView
将图片加载到Picasso
。我搜索了很多,但我找不到答案。
有GridView
,GridView有ImageView
和TextView
。我使用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
我错了什么?
答案 0 :(得分:0)
你没有做错,你的图像是错误的。我想这是因为它没有延伸或在请求被拍摄后没有立即形成。您的图像是使用某种CG库形成的,它会在请求完成后生成图像,因此正文将返回空白。
答案 1 :(得分:0)
您大多错过了INTERNET权限。当你试图调试Picasso时,我建议:
像这样创建一个Builder:
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);
您将在日志中看到是否有任何错误。祝你好运。