[英]holder returning wrong view
I uploading some list from server. 我从服务器上传一些列表。 I showing it through 'Adapter'.
我通过“适配器”显示它。 Then I loading image of every list element in background.
然后,我在后台加载每个列表元素的图像。
After loading every images I calling notifyDataSetChanged() from Adapter and accordingly getView() has been called for refresh every image for each list element. 加载每个图像之后,我从Adapter调用notifyDataSetChanged(),并因此调用了getView()来刷新每个列表元素的每个图像。
Problem is that I have updated image just for the last list element. 问题是我只为最后一个列表元素更新了图像。 I understanding that "holder = (ViewHolder) rowView.getTag();"
我了解到“ holder =(ViewHolder)rowView.getTag();” returning holder for another view.
返回持有人的另一种观点。
But if I uncommenting two lines below: 但是,如果我在下面取消注释两行:
// 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; }
}
You have to create a holder for every view... 您必须为每个视图创建一个支架...
...
rowView = inflater.inflate(R.layout.applist_item, null, true);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.tvAppName);
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.