当ListView项目离开Border时,适配器的奇怪反应

时间:2014-06-25 13:37:48

标签: android adapter

请看看这个适配器,我有一个奇怪的问题,它从数据库中读取数据,当行在列表视图的边界时,它们找到并没有问题,但是当它们去的时候在listview边界之外(所以我必须向下滚动)每一件东西都混淆了,没有什么东西在它的位置上,你们这些人都知道了吗?

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

@Override
public Object getItem(int position) {
    return mCursor.getString(position);
}

@Override
public long getItemId(int position) {
    return mCursor.getPosition();
}
// mCursor.getPosition()

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

        holder = new ViewHolder();
        convertView = mLayoutInflater.inflate(R.layout.listitem_discuss, 
 null);
          this.convertview=convertView;


    mCursor.moveToPosition(position);

    holder.wrapper = (LinearLayout) convertView.findViewById(R.id.wrapper);

    holder.theMessage = (TextView) convertView.findViewById(R.id.comment);
    holder.theName = (TextView) convertView.findViewById(R.id.MSGname);
    holder.theImage = (ImageView)convertView.findViewById(R.id.MSGimage);

    android.widget.FrameLayout.LayoutParams lp =  
  (android.widget.FrameLayout.LayoutParams) holder.theName.getLayoutParams();



     String Namer= mCursor.getString(mCursor.getColumnIndex("username"));
           String namer[] = Namer.split("\\@");
           String imagenamer=namer[0];

    holder.theName.setText(imagenamer);

 holder.theMessage.setText(mCursor.getString(mCursor.getColumnIndex("message")));

    int isright=  
 Integer.valueOf(mCursor.getString(mCursor.getColumnIndex("isright")));

    if(isright==0){

        holder.theMessage.setBackgroundResource(R.drawable.bubble_yellow);

        holder.wrapper.setGravity(Gravity.LEFT);

        lp.gravity = Gravity.LEFT;
        holder.theName.setLayoutParams(lp);

        android.widget.FrameLayout.LayoutParams params = new 
 android.widget.FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT     ,Gravity.LEFT);
        holder.theImage.setLayoutParams(params);

        holder.theImage.setPadding(10,0,0,0);
        holder.wrapper.setPadding( ImageWidth+10,40,0, 0);
        holder.theName.setPadding( ImageWidth+25, 0,0, 0);
        //(left, top, right, bottom)
        File file= new File("storage/sdcard/LifeMatePrivate/ProfileImage
/ProfileImage,imagechange_2,"+imagenamer+",.jpg");
        if(file.exists()){
        Picasso.with(mContext).load(new File("storage/sdcard
/LifeMatePrivate/ProfileImage
/ProfileImage,imagechange_2,"+imagenamer+",.jpg")).resize(ImageWidth,   
ImageHeight).centerCrop().into(holder.theImage);
        }else{
        Picasso.with(mContext).load(new File("storage/sdcard
/LifeMatePrivate/ProfileImage/Default.jpg")).resize(ImageWidth,  
ImageHeight).centerCrop().into(holder.theImage);
        }



    }else{

    holder.theMessage.setBackgroundResource(R.drawable.bubble_green);

    holder.wrapper.setGravity(Gravity.RIGHT);

    lp.gravity = Gravity.RIGHT;
    holder.theName.setLayoutParams(lp);

    android.widget.FrameLayout.LayoutParams params = new 
android.widget.FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
 ,Gravity.RIGHT);
    holder.theImage.setLayoutParams(params);

    holder.theImage.setPadding(0,0,10,0);
    holder.wrapper.setPadding(0,40, ImageWidth+10, 0);
    holder.theName.setPadding(0, 0, ImageWidth+25, 0);
    //(left, top, right, bottom)
    File file= new File("storage/sdcard/LifeMatePrivate/ProfileImage  
/ProfileImage,imagechange_2,"+imagenamer+",.jpg");
    if(file.exists()){
    Picasso.with(mContext).load(new File("storage/sdcard/LifeMatePrivate
 /ProfileImage/ProfileImage,imagechange_2,"+imagenamer+",.jpg")).resize(ImageWidth, 
 ImageHeight).centerCrop().into(holder.theImage);
    }else{
    Picasso.with(mContext).load(new File("storage/sdcard/LifeMatePrivate
 /ProfileImage/Default.jpg")).resize(ImageWidth, 
 ImageHeight).centerCrop().into(holder.theImage);
    }

    }


    if(!mCursor.isLast()){
        mCursor.moveToNext();   
    }

    convertView.setTag((ViewHolder)holder);

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

1 个答案:

答案 0 :(得分:0)

嗯,ViewHolder的目的是每次都不调用FindViewbyId(),每当你查看滚出边框时,android都会回收视图,所以你必须重建它。通过更改getView的第一部分来修复问题:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
   if (convertView == null) {
    System.out.println("convertview null");
        holder = new ViewHolder();
        convertView = mLayoutInflater.inflate(R.layout.listitem_discuss, null);
          this.convertview=convertView;




    holder.wrapper = (LinearLayout) convertView.findViewById(R.id.wrapper);

    holder.theMessage = (TextView) convertView.findViewById(R.id.comment);
    holder.theName = (TextView) convertView.findViewById(R.id.MSGname);
    holder.theImage = (ImageView)convertView.findViewById(R.id.MSGimage);


    convertView.setTag((ViewHolder)holder);

   }else {
       holder = (ViewHolder) convertView.getTag();
    System.out.println("convertview not null");
   }
  ////rest of the code