我使用的ListView是使用包含ImageView和TextView的Layout制作的。这是我的适配器的代码
public class ResultsArrayAdapter extends ArrayAdapter<ImageText> implements Picasso.Listener {
private static final String LOG_TAG = ResultsArrayAdapter.class.getSimpleName();
private Context mContext;
private int mLayoutId;
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Log.e(LOG_TAG, "Image load fail: " + uri.toString() + ", message: " + exception.getMessage());
}
public ResultsArrayAdapter(Context context, int resourceId, int layoutId, List<ImageText> data) {
super(context, resourceId, layoutId, data);
mContext = context;
mLayoutId = layoutId;
}
private class ViewHolder {
public ImageView imageView;
public TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
ImageText imageText = getItem(position);
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(mLayoutId, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageview_result);
viewHolder.textView = (TextView)convertView.findViewById(R.id.textview_result);
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder)convertView.getTag();
Picasso picasso = new Picasso.Builder(mContext).build();
picasso.load(imageText.getImageURL())
.placeholder(R.drawable.artistplaceholder)
.error(R.drawable.error).fit().into(viewHolder.imageView);
viewHolder.textView.setText(imageText.getText());
return convertView;
}
}
ImageText包含两个字符串,一个用于图像URL,另一个用于文本。我从spotify获取数据。我检查了imageText.getImageURL()返回实际的图片网址,它在我的浏览器中打开了一张图片。现在,应用程序用文本填充ListView,并在每个条目上放置一个占位符。 onImageLoadFailed没有被调用。这是Picasso图书馆http://square.github.io/picasso/的链接。我使用的是2.3.3版本,它是使用gradle脚本编译的,行编译为“com.squareup.picasso:picasso:2.3.3”。添加了互联网权限,证据就是我能够获取名称。
感谢您的帮助。
也许它也会影响结果。这是我的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<ImageView
android:layout_width="@dimen/general_picture_side"
android:layout_height="@dimen/general_picture_side"
android:padding="@dimen/general_picture_padding"
android:id="@+id/imageview_result"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/text_picture_offset"
android:id="@+id/textview_result"/>
</LinearLayout>