1

嘿伙计们,我有一个视图支架 + 一个基本适配器,但它不起作用

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    // get the selected entry
    AI entry = (AI) mListAppInfo.get(position);

    // inflate new layout if null
    if(v == null) {           
        holder = new ViewHolder();
        v = inflater.inflate(R.layout.list, null);

        holder.position = position;
        holder.title = (TextView)v.findViewById(R.id.tvName);
        holder.icon = (ImageView)v.findViewById(R.id.ivIcon);
        holder.iconimg = entry.icon;

        new ThumbnailTask(position, holder)
        .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);

        v.setTag(holder);   
    }else{
        holder = (ViewHolder) v.getTag();
    }

    holder.title.setText(entry.title);

    // return view
    return v;
}

列表中的项目没有显示任何我做错了什么?

编辑这是全班

public class AIAdapter extends BaseAdapter {
    private Context mContext;
    private List<?> mListAppInfo;
    private LayoutInflater inflater;

    public AIAdapter(Context c, List<?> list, PackageManager pm) {
        mContext = c;
        mListAppInfo = list;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mListAppInfo.size();
    }

    @Override
    public Object getItem(int position) {
        return mListAppInfo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("null")
    @Override
    public View getView(int position, View v, ViewGroup parent) {
        ViewHolder holder;
        // get the selected entry
        AI entry = (AI) mListAppInfo.get(position);

        // inflate new layout if null
        if(v == null) {           
            holder = new ViewHolder();
            v = inflater.inflate(R.layout.list, null);

            holder.position = position;
            holder.title = (TextView)v.findViewById(R.id.tvName);
            holder.icon = (ImageView)v.findViewById(R.id.ivIcon);
            holder.iconimg = entry.icon;

            new ThumbnailTask(position, holder)
            .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);

            v.setTag(holder);   
        }else{
            holder = (ViewHolder) v.getTag();
        }

        holder.title.setText(entry.title);

        // return view
        return v;
    }

    @SuppressWarnings("rawtypes")
    private static class ThumbnailTask extends AsyncTask{
        private int mPosition;
        private ViewHolder mHolder;
        private Rect mOldBounds = new Rect();

        public ThumbnailTask(int position, ViewHolder holder) {
            mPosition = position;
            mHolder = holder;
        }

        @Override
        protected Drawable doInBackground(Object... arg0) {
            Drawable icon = mHolder.iconimg;

            int width = 64;
            int height = 64;

            final int iconWidth = icon.getIntrinsicWidth();
            final int iconHeight = icon.getIntrinsicHeight();

            if (icon instanceof PaintDrawable) {
                PaintDrawable painter = (PaintDrawable) icon;
                painter.setIntrinsicWidth(width);
                painter.setIntrinsicHeight(height);
            }

            if (width > 0 && height > 0 && (width < iconWidth || height < iconHeight)) {
                final float ratio = (float) iconWidth / iconHeight;

                if (iconWidth > iconHeight) {
                    height = (int) (width / ratio);
                } else if (iconHeight > iconWidth) {
                    width = (int) (height * ratio);
                }

                final Bitmap.Config c =
                        icon.getOpacity() != PixelFormat.OPAQUE ?
                            Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
                final Bitmap thumb = Bitmap.createBitmap(width, height, c);
                final Canvas canvas = new Canvas(thumb);
                canvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, 0));
                mOldBounds.set(icon.getBounds());
                icon.setBounds(0, 0, width, height);
                icon.draw(canvas);
                icon.setBounds(mOldBounds);
                icon = new BitmapDrawable(thumb);
            }

            return icon;
        }

        protected void onPostExecute(Drawable bitmap) {
            if (mHolder.position == mPosition) {
                mHolder.icon.setImageDrawable(bitmap);
            }
        }

    }
}

class ViewHolder{
    public int position;
    public TextView title;
    public ImageView icon;
    public Drawable iconimg;
}

我不知道我做错了什么。所有 entry.title 和 entry.icon 都有数据。虽然列表视图上没有显示任何内容。布局列表确实有一个 imageview 和 textview。这在没有查看器的情况下有效,但是一旦我添加了查看器,它就不会

4

2 回答 2

2

一方面不需要创建“v”对象,你可以直接使用convertView。您是否正确覆盖了 BaseAdapter 类?例如 getCount() 方法...它是否返回 mListAppInfo.size()?

乍一看,您的方法应该有效;你可以复制/粘贴你的整个班级吗?

于 2012-12-11T18:36:41.983 回答
1

应用 viewHolder 模式时,您的 ViewHolder 类需要是静态的;

现在有点离题了,你应该小心那些你经常发出的异步任务,你可能要考虑使用线程池。同样,尝试考虑 SparseArray 或 HashMap 而不是适配器中的项目列表,它们在检索给定索引处的项目时比 List 更快。

尝试在这一行设置一个刹车点:“ holder.title.setText(entry.title);” 它到达那里了吗?所有信息都正确吗?

于 2012-12-11T19:29:39.560 回答