列出视图单元回收问题

时间:2014-01-24 09:15:50

标签: android listview recycle

在我的案例列表视图中滚动显示重复的单元格。不能正常工作。这是我在getView()

中的代码
    if(convertView == null)
        {
            LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.contact_cell, null);
        }

        try {
            if(contacts.getContacts().get(position).getPhone() != null) {
                ImageView contactIcon = (ImageView) convertView.findViewById(R.id.pic);
                TextView contactName = (TextView) convertView.findViewById(R.id.name);
                contactName.setText(contacts.getContacts().get(position).getDisplayName());
                contactIcon.setImageBitmap(contacts.getContacts().get(position).getThumbNail());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return convertView;

2 个答案:

答案 0 :(得分:1)

我想这是你的问题:

if(contacts.getContacts().get(position).getPhone() != null)

如果手机为空,你在做什么? 您将旧内容保留在视图中。您应该为这种情况提供一些占位符。 异常也是如此。顺便问一下你为什么要在那里捕捉异常?

答案 1 :(得分:1)

将此添加到您的getView()方法

if(contacts.getContacts().get(position).getPhone() != null) {
    ImageView contactIcon = (ImageView) convertView.findViewById(R.id.pic);
    TextView contactName = (TextView) convertView.findViewById(R.id.name);
    contactName.setText(contacts.getContacts().get(position).getDisplayName());
    contactIcon.setImageBitmap(contacts.getContacts().get(position).getThumbNail());
}else{
    ImageView contactIcon = (ImageView) convertView.findViewById(R.id.pic);
    TextView contactName = (TextView) convertView.findViewById(R.id.name);
    contactName.setText("");
    contactIcon.setImageBitmap(null);
}

getPhone()返回null且convertView未更新时,会发生这种情况。