我相信遵循标准模式以避免回收的listview数据,但是,我失败了。像往常一样,列表视图的最后两项(低于折叠......从其他先前的位置获取数据)
public static class ViewHolder {
public ImageView img1;
public ImageView img2;
public TextView text1;
public TextView text2;
public TextView text3;
public TextView text4;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if( convertView == null ){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.text2=(TextView)vi.findViewById(R.id.text2);
holder.text3=(TextView)vi.findViewById(R.id.text3);
holder.text4=(TextView)vi.findViewById(R.id.text4);
holder.img1=(ImageView)vi.findViewById(R.id.img1);
holder.img2=(ImageView)vi.findViewById(R.id.img2);
vi.setTag(holder);
} else {
holder=(ViewHolder)vi.getTag();
}
String shorten=shortenText(t4);
holder.text.setTag(shorten);
holder.text.setText(shorten);
if(t1){
holder.text1.setTag(thisMsg.get(D_ENUM.T1));
holder.innerName.setText(thisMsg.get(thisMsg.get(D_ENUM.T1));
}
if(t2){
holder.text1.setTag(thisMsg.get(D_ENUM.T2));
holder.innerName.setText(thisMsg.get(thisMsg.get(D_ENUM.T2));
}
if(thisMsg.containsKey(D_ENUM.T3)){
String t3 = shortenText(thisMsg.get(D_ENUM.T3));
holder.innerDesc.setTag(t3);
holder.innerDesc.setText(t3);
}
holder.img1.setTag(img1);
imageLoader.DisplayImage(img1, act, holder.img1);
if(img2 != null) {
holder.innerImage.setTag(img2);
holder.innerImage.setVisibility(View.VISIBLE);
imageLoader.DisplayImage(img2, act, holder.img2);
} else {
holder.innerImage.setTag(img2);
holder.innerImage.setVisibility(View.GONE);
}
return vi;
答案 0 :(得分:3)
您必须为ViewHolder的所有成员设置内容,即使这意味着如果您没有任何要显示的内容,也要将其设置为空值。
了解您之前执行的getView函数已填充了您传递的视图(convertView
)和关联的ViewHolder(holder
);它们不是新创建的具有默认值的对象