简体   繁体   中英

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.

Problem is that I have updated image just for the last list element. I understanding that "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);
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM