3

我已经在下面包含了我的 listview 适配器的代码,除了图像不是从 url 加载的之外,所有的工作都很好。可能是什么情况。我正在使用 Android Query 库来加载图像。

  public Onadapter(Context context,String[] id,String[] label,String[] title,String[] image,String[] hrs,String[] posted_date)
    {
        this.context=context;
        this.id = id;
        this.label = label;
        this.title = title;
        this.image = image;
        this.hrs = hrs;
        this.posted_date=posted_date;


    }

    private class ViewHolder{
        ImageView img;
        TextView id;
        TextView label;
        TextView title;
        TextView hrs;
        TextView posted_date;

    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.whatslist, null);
            holder = new ViewHolder();
            holder.id=(TextView) convertView.findViewById(R.id.whats_id);
            holder.img = (ImageView) convertView.findViewById(R.id.thumb1);
            holder.label= (TextView) convertView.findViewById(R.id.label);
            holder.title = (TextView) convertView.findViewById(R.id.artist_e_desc);
            holder.hrs = (TextView) convertView.findViewById(R.id.hrs);
            holder.posted_date = (TextView) convertView.findViewById(R.id.date);

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

        holder.id.setText(id[position]);
        holder.label.setText(label[position]);
        holder.title.setText(title[position]);
        holder.hrs.setText(hrs[position]);
        holder.posted_date.setText(posted_date[position]);
        imgaq = new AQuery(convertView);
        imgaq.id(holder.img).image(image[position], true, true, 0, 0, null, AQuery.FADE_IN_NETWORK, 1.0f);
        return convertView;
    }


    @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return image.length;
        }


        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }




}
4

3 回答 3

5

也许你最好使用 recycle()

我认为在 getView() 方法中每次都不是新的好方法。

AQuery listAq = new AQuery(this);

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.whatslist, null);
        holder = new ViewHolder();
        holder.id=(TextView) convertView.findViewById(R.id.whats_id);
        holder.img = (ImageView) convertView.findViewById(R.id.thumb1);
        holder.label= (TextView) convertView.findViewById(R.id.label);
        holder.title = (TextView) convertView.findViewById(R.id.artist_e_desc);
        holder.hrs = (TextView) convertView.findViewById(R.id.hrs);
        holder.posted_date = (TextView) convertView.findViewById(R.id.date);

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

    holder.id.setText(id[position]);
    holder.label.setText(label[position]);
    holder.title.setText(title[position]);
    holder.hrs.setText(hrs[position]);
    holder.posted_date.setText(posted_date[position]);

    AQuery imgaq = listAq.recycle(convertView);
    imgaq.id(holder.img).image(image[position], true, true, 0, 0, null, AQuery.FADE_IN_NETWORK, 1.0f);
    return convertView;
}
于 2014-05-26T03:50:06.920 回答
0

我会写这一行:

imgaq = new AQuery(convertView);

在适配器的构造函数中并将上下文传递给 AQuery 构造函数而不是视图。尤其是 convertView 有时为空,所以你用空初始化 AQuery

  public Onadapter(Context context,String[] id,String[] label,String[] title,String[] image,String[] hrs,String[] posted_date)
{
    this.context=context;
    this.id = id;
    this.label = label;
    this.title = title;
    this.image = image;
    this.hrs = hrs;
    this.posted_date=posted_date;

    this.aq = new AQuery(context);

}
于 2013-08-01T10:43:46.507 回答
0

原来我错误地引用了 imageview 的 xml 资源。我很傻:D

于 2013-08-02T10:26:17.017 回答