我有一個GridView與圖像和TextViews。使用異步任務從URL加載圖像。GridView圖像加載不正確
這裏是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或更少。加載更多圖像時,會出現問題,例如圖像重複位置錯誤,很少圖像未加載。但是要加載的圖片數量沒問題。 不知道是什麼導致了這個問題。
感謝您的幫助
這可能會幫助你http://stackoverflow.com/questions/21029223/loading-images-in-a-gridview-with -async-task-not-loading-correctly –