【问题标题】:wrong images are displaying first and then correct images are displaying during scrolling of listview首先显示错误的图像,然后在滚动列表视图期间显示正确的图像
【发布时间】:2012-11-03 21:18:20
【问题描述】:

我在我的应用程序中使用带有图像的列表视图。但每当我 滚动列表首先显示错误的图像,然后显示正确的图像 图像正在显示。我不知道为什么会这样。我 认为已经加载的图像正在显示,直到设置位置。 这是我的适配器类,请帮助我哪里出错了

  public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private Activity context;
    ArrayList<String> namear,msgar,idar,profimage,postimage,commentsnum,objectid,urlString;
  ImageLoader imageLoader;
  Bitmap[] bitdata;

  public MySimpleArrayAdapter(Activity c,int i,ArrayList<String> postpic, ArrayList<String> names,ArrayList<String> msg,ArrayList<String> id,ArrayList<String> proimg,Bitmap[] bit,ArrayList<String> comment,ArrayList<String> objid,ArrayList<String> web) {

      super(c, i, names);
    Log.e("adapter","adap");
    this.context = c;
    this.namear = names;
    this.msgar = msg;
    this.idar = id;
    this.profimage=proimg;
    this.postimage=postpic;
    this.bitdata=bit;
    this.commentsnum=comment;
    this.objectid=objid;
    this.urlString=web;
   this.imageLoader = new ImageLoader(context);
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
  View rowView=convertView;
  ViewHolder holder = new ViewHolder();
      if(convertView == null) {
  LayoutInflater inflator = getLayoutInflater();
  rowView = inflator.inflate(R.layout.walldata, null);

  holder.name1 = (TextView) rowView.findViewById(R.id.name);
  holder.message1 = (TextView) rowView.findViewById(R.id.msg);
  holder.profimg= (ImageView) rowView.findViewById(R.id.profile_pic);
  holder.postimg= (ImageView) rowView.findViewById(R.id.picpost);
  holder.comments = (TextView) rowView.findViewById(R.id.comment);
  rowView.setTag(holder);
     Log.e("user",idar.get(position));
      }
      else
         {
          holder = (ViewHolder) rowView.getTag();
         }
    Log.e("adapter","adap");
    holder.name1.setText(namear.get(position));
    if(msgar.get(position)!=""){
        holder.message1.setText(msgar.get(position));

    }
    else
    {
        holder.message1.setVisibility(View.GONE);
    }
    if(!postimage.get(position).equals(""))
    {try{
         imageLoader.DisplayImage(postimage.get(position).replace(" ", "%20"), holder.postimg) ;

    }
    catch(Exception e){
        e.printStackTrace();
    }
    }
    else
    {
         holder.postimg.setVisibility(View.GONE);
    }
    try{
      imageLoader.DisplayImage(profimage.get(position).replace(" ", "%20"), holder.profimg) ;
    }
    catch(Exception e){
       e.printStackTrace();
    }

    holder.comments.setText(commentsnum.get(position)+"\t"+"Comments");
    holder.comments.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myintent=new Intent(Wall.this,Comments.class);
            myintent.putExtra("name", namear.get(position));
            myintent.putExtra("profimg", profimage.get(position));
            myintent.putExtra("message", msgar.get(position));
            myintent.putExtra("postpic", postimage.get(position));
            myintent.putExtra("objectid", objectid.get(position));
            myintent.putExtra("commentsnum",commentsnum.get(position));
            myintent.putExtra("webservice", urlString.get(position));
            startActivity(myintent);

        }
    });

    return rowView;
  }

}
public class ViewHolder{
    TextView name1, message1,comments;
    ImageView profimg,postimg;
}

【问题讨论】:

标签: android android-listview android-adapter


【解决方案1】:

确保在 getView 方法中尽早将 ImageView 中的图像设置为 null。由于 ListView 正在重复使用列表项,因此将显示旧的,直到设置了新的。

【讨论】:

  • 当我向下再向上滚动时,已经加载的图像没有显示,显示需要时间
  • 如果您正在下载图像,您应该实现缓存以避免每次显示时都下载它们。只需在本地读取它们并将它们设置为 ImageView 就很快。
  • 我以不同的方法工作,没有采用任何这些持有者类,什么也没有,在快速滚动列表视图期间,位图大小超出 vm 预算出现内存不足错误
  • 我看不出这与您上面的问题有什么关系。您仍然可以使用相同的方法,使用 holder 类,只需添加缓存并确保删除旧图像以避免在滚动时加载新图像之前看到它。也许我不完全明白你的问题是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 2019-04-01
  • 2013-03-26
  • 1970-01-01
相关资源
最近更新 更多