我从服务器上传一些列表。我通过适配器'来展示它。然后 我在后台加载每个列表元素的图像。
加载每个图像后,我从Adapter调用notifyDataSetChanged() 并因此调用getView()来刷新每个图像 每个列表元素。
问题是我已经为最后一个列表元素更新了图像。一世 理解" holder =(ViewHolder)rowView.getTag();"回国 持有人的另一种观点。
但如果我取消注释以下两行:
// holder.textView = (TextView) rowView.findViewById(R.id.tvAppName);
// holder.imageView = (ImageView) rowView.findViewById(R.id.ivImage);
I getting expected result; but I don't want always call findViewByld.
private static class ViewHolder {
public ImageView imageView;
public TextView textView;
public int rowCount = 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.applist_item, null, true);
holder.textView = (TextView) rowView.findViewById(R.id.tvAppName);
holder.imageView = (ImageView) rowView.findViewById(R.id.ivImage);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
// holder.textView = (TextView) rowView.findViewById(R.id.tvAppName);
// holder.imageView = (ImageView) rowView.findViewById(R.id.ivImage);
}
ApkData app = getItem(position);
holder.textView.setText(app.getAppName());
Drawable mPic = loadImage(app);
holder.imageView.setImageDrawable(mPic);
return rowView;
}
private Drawable loadImage(final ApkData item) {
if (item.getIconImage() != null) {
Log.v(Constants.TAG, "item.getIconImage() != null");
return item.getIconImage();
} else {
Log.v(Constants.TAG, "item.getIconImage() == NULL");
}
mExecutor.execute(new Runnable() {
@Override
public void run() {
if (!(Thread.currentThread().isInterrupted())) {
Drawable res = null;
Bitmap image = null;
int tryCount = 0;
while ((image == null) && (tryCount <= Constants.TRY_CONNECCTION)) {
image = Utils.loadImage(item.getIconPath(),
holder.imageView.getLayoutParams().height,
holder.imageView.getLayoutParams().width);
tryCount++;
}
if (image != null) {
res = new BitmapDrawable(getResources(), image);
}
if (res == null) {
res = getResources().getDrawable(R.drawable.android_app_icon_logo);
}
item.setIconImage(res);
asyncNotifyDataChanged();
}
}
});
return getResources().getDrawable(R.drawable.android_app_icon_logo);
}
//Utils.loadImage:
public static Bitmap loadImage(String urlStr, int height, int width) {
if (!urlStr.startsWith("http://") && !urlStr.startsWith("https://"))
urlStr = "http://" + urlStr;
URL url;
try {
url = new URL(urlStr);
Bitmap mImg = null;
mImg = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return scaleImage(mImg, height, width);
} catch (MalformedURLException e) {
Log.e(Constants.TAG, "loadImage MalformedURLException:", e);
return null;
} catch (IOException e1) {
Log.e(Constants.TAG, "loadImage IOException:", e1);
return null;
} catch (NullPointerException ex) {
Log.e(Constants.TAG, "loadImage NullPointerException:", ex);
return null; }
}
答案 0 :(得分:0)
您必须为每个视图创建一个持有者......
...
rowView = inflater.inflate(R.layout.applist_item, null, true);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.tvAppName);
....