簡體   English   中英

持有人返回錯誤的觀點

[英]holder returning wrong view

我從服務器上傳一些列表。 我通過“適配器”顯示它。 然后,我在后台加載每個列表元素的圖像。

加載每個圖像之后,我從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;        }
    }

您必須為每個視圖創建一個支架...

...
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM