我有一个带有Image和TextViews的GridView。使用异步任务从URL加载图像。
以下是GridView的代码:
public class GridviewAdapter extends BaseAdapter {
private ArrayList<String> listImgUrl;
private ArrayList<String> listCost;
private ArrayList<String> listItem;
private ArrayList<String> listStore;
private Activity activity;
// private Context context;
public GridviewAdapter(Context context) {
super();
// this.context = context;
}
public void GridviewAdapterInit(Activity activity, ArrayList<String> listImgUrl,
ArrayList<String> listCost, ArrayList<String> listItem,
ArrayList<String> listStore) {
this.activity = activity;
this.listImgUrl = listImgUrl;
this.listCost = listCost;
this.listItem = listItem;
this.listStore = listStore;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listImgUrl.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listImgUrl.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder {
public ImageView imgViewFlag;
public TextView tvCost;
public TextView tvItem;
public TextView tvStore;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview_row, null);
view.imgViewFlag = (ImageView) convertView
.findViewById(R.id.imageView1);
view.tvCost = (TextView) convertView.findViewById(R.id.tvCost);
view.tvItem = (TextView) convertView.findViewById(R.id.tvItem);
view.tvStore = (TextView) convertView.findViewById(R.id.tvStore);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
LoadImageFromURL(listImgUrl.get(position), view.imgViewFlag);
view.tvCost.setText(listCost.get(position));
view.tvItem.setText(listItem.get(position));
view.tvStore.setText(listStore.get(position));
return convertView;
}
private void LoadImageFromURL(String url, ImageView imgView)
{
new ImageViewUpdator(imgView).execute(url);
}
}
使用上述代码,如果图像为6或更小,则正确加载图像。当加载更多图像时,存在诸如图像在错误位置重复,没有加载少量图像的问题。但是要加载的图像数量是可以的。 不确定是什么导致了这个问题。
感谢您的帮助
答案 0 :(得分:1)
如果您想从网址加载图片,则需要使用picasso
Picasso是一个功能强大的图像下载和缓存库 机器人。
只需一行代码即可解决您的图片加载问题:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);