【问题标题】:List view cell recycling issue列表视图单元回收问题
【发布时间】:2014-02-15 05:06:54
【问题描述】:

在我的情况下,滚动显示重复单元格的列表视图。不能正常工作。这是我在 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;

【问题讨论】:

    标签: android listview recycle


    【解决方案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 没有更新时会发生这种情况。

    【讨论】:

      【解决方案2】:

      我猜这是你的问题:

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

      如果手机是空的,你在做什么? 您将旧内容留在视图中。您应该为此条件提供一些占位符。 例外情况相同。顺便说一句,你为什么会在那里发现异常?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-09
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-18
        • 2023-04-01
        • 1970-01-01
        相关资源
        最近更新 更多