我有一个自定义列表视图适配器,当我向下/向上滚动列表时,图像被打乱了,我不知道该怎么办。如何解决这个问题?
public View getView(int position, View convertView, ViewGroup parent) {
DataModel dataModel = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
convertView=LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.letter = convertView.findViewById(R.id.tvFirstt);
viewHolder.name = convertView.findViewById(R.id.nameview);
viewHolder.contact =convertView.findViewById(R.id.contactview);
viewHolder.profileimg=convertView.findViewById(R.id.profileimg);
viewHolder.chkSelect = convertView.findViewById(R.id.checkbox);
viewHolder.detail = convertView.findViewById(R.id.detail);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(dataModel.fname + " " + dataModel.lname);
viewHolder.contact.setText(dataModel.landline);
viewHolder.detail.setImageResource(R.drawable.right);
viewHolder.chkSelect.setTag(position);
viewHolder.chkSelect.setChecked(dataModel.isChecked);
viewHolder.chkSelect.setOnCheckedChangeListener(this);
if (dataModel.image == null) {
viewHolder.letter.setVisibility(View.VISIBLE);
viewHolder.letter.setText(dataModel.fname.substring(0, 1));
} else {
viewHolder.profileimg.setVisibility(View.VISIBLE);
viewHolder.profileimg.setImageBitmap(convertToBitmap(dataModel.image));
}
} else {
Log.e("Error", "Please see again");
}
convertView.setTag(viewHolder);
return convertView;
}
答案 0 :(得分:0)
您没有重置图像
public View getView(int position, View convertView, ViewGroup parent) {
DataModel dataModel = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
convertView=LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.letter = convertView.findViewById(R.id.tvFirstt);
viewHolder.name = convertView.findViewById(R.id.nameview);
viewHolder.contact =convertView.findViewById(R.id.contactview);
viewHolder.profileimg=convertView.findViewById(R.id.profileimg);
viewHolder.chkSelect = convertView.findViewById(R.id.checkbox);
viewHolder.detail = convertView.findViewById(R.id.detail);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(dataModel.fname + " " + dataModel.lname);
viewHolder.contact.setText(dataModel.landline);
viewHolder.detail.setImageResource(R.drawable.right);
viewHolder.chkSelect.setTag(position);
viewHolder.chkSelect.setChecked(dataModel.isChecked);
viewHolder.chkSelect.setOnCheckedChangeListener(this);
if (dataModel.image == null) {
viewHolder.letter.setVisibility(View.VISIBLE);
viewHolder.letter.setText(dataModel.fname.substring(0, 1));
//I have added this
viewHolder.profileimg.setImageBitmap(null);
} else {
viewHolder.profileimg.setVisibility(View.VISIBLE);
viewHolder.profileimg.setImageBitmap(convertToBitmap(dataModel.image));
}
} else {
Log.e("Error", "Please see again");
}
convertView.setTag(viewHolder);
return convertView;
}