Scroll Android时ListView数据错位

时间:2014-09-04 08:48:51

标签: android listview scroll

这个问题可能会被多次询问,但我无法解决问题。每当我滚动自定义ListView数据是错位的我在哪里放了If else条件。请让我知道我的错误。

公共类ImageAdapter扩展了BaseAdapter {

private LayoutInflater mInflater;

public ImageAdapter(Context c) {
    mInflater = LayoutInflater.from(c);

}

@Override
public Object getItem(int position) {
    return null;
}

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

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



    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.griddata, null);
        k = position;
        holder = new ViewHolder();
        holder.txttitle = (TextView) convertView
                .findViewById(R.id.txttitle);
        holder.txtproduct = (TextView) convertView
                .findViewById(R.id.txtproduct);
        holder.txtcustname = (TextView) convertView
                .findViewById(R.id.txtcustname);
        holder.txtcompanyname = (TextView) convertView
                .findViewById(R.id.txtcompanyname);
        holder.txtaddress = (TextView) convertView
                .findViewById(R.id.txtaddress);
        holder.txttime = (TextView) convertView
                .findViewById(R.id.txttime);
        holder.txtrefid = (TextView)convertView.findViewById(R.id.txtrefid);
        holder.btnphoto1 = (Button)convertView.findViewById(R.id.btnphoto1);
        holder.btnphoto2 = (Button)convertView.findViewById(R.id.btnphoto2);
        holder.btnsignpad = (Button)convertView.findViewById(R.id.btnsignature);

        if (imgurl1list.get(position).equals("anyType{}")) {
            holder.btnphoto1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    i = 1;
                    type = title.get(position).toString();

                    refid = v.getTag().toString();
                    selectedbutton = holder.btnphoto1;

                    selectImage();  

                }
            });
        } 
        else{
            holder.btnphoto1.setText("Photo Exists");
        }


        if (imgurl2list.get(position).equals("anyType{}")) {
            holder.btnphoto2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    i = 2;
                    type = title.get(position).toString();
                    refid = v.getTag().toString();
                    selectedbutton = holder.btnphoto2;
                    selectImage();
                }
            });
        }
        else{
            holder.btnphoto2.setText("Photo Exists");
        }
        if (imgurl3list.get(position).equals("anyType{}")) {
            holder.btnsignpad.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    type = title.get(position).toString();
                    refid = v.getTag().toString();
                    selectedbutton = holder.btnsignpad;
                    signpadselection();
                }
            });     
        } 
        else{
            holder.btnsignpad.setText("Signature Exists");
        }

        convertView.setTag(holder);
    }

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


    holder.txttitle.setText(title.get(position));
    holder.txtrefid.setText(uniquerefid.get(position));
    holder.txttitle.setTag(uniquerefid.get(position));
    holder.txtaddress.setText(address.get(position));
    holder.txtcustname.setText(customername.get(position));
    holder.txtcompanyname.setText(companyname.get(position));
    holder.txtproduct.setText(product.get(position));
    holder.txttime.setText(createdon.get(position));
    holder.btnphoto1.setTag(uniquerefid.get(position));
    holder.btnphoto2.setTag(uniquerefid.get(position));
    holder.btnsignpad.setTag(uniquerefid.get(position));




    return convertView;
}

class ViewHolder {
    TextView txttitle, txtcompanyname, txtproduct, txtcustname,
            txtaddress, txttime, txtrefid;
    Button btnphoto1, btnphoto2, btnsignpad;

}

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

}

1 个答案:

答案 0 :(得分:0)

setOnClickListener分配的侦听器中,绑定到position,在创建视图时,第一次调用getItem()时传递了该position。因此,即使滚动,holder仍然保持不变。您应该将其存储在holder.position = position; 中:

title.get(position).toString();

并改变

title.get(holder.position).toString();

final

要确保您更改了所有内容,请从position参数中删除getItemId()限定符,因为它不再是必需的。

另外,在position中返回0并不是一个好主意(虽然我不确定它会影响滚动,但肯定会影响其他事情)。最好返回{{1}}。