为什么 getView 在分离列表适配器上返回错误的 ConvertView 对象?

2024-04-24

我根据自己的需要改编了 Jeff Sharkey 的分离列表适配器 (SeparatedListAdapter),得到了如下结果:

public class SeparatedListAdapter<T> extends BaseAdapter {

    @SuppressWarnings("unused")
    private final String LOG_TAG = getClass().getSimpleName();

    public final static int TYPE_SECTION_HEADER = 0;

    public final Map<T, Adapter> sectionAdapters;
    public final ArrayAdapter<T> headerAdapter;

    public SeparatedListAdapter(ArrayAdapter<T> headerAdapter) {
        super();
        this.sectionAdapters = new LinkedHashMap<T,Adapter>();
        this.headerAdapter = headerAdapter;
    }

    public void addSection(T section, Adapter adapter) {
        this.headerAdapter.add(section);
        this.sectionAdapters.put(section, adapter);
    }

    public void clearSections() {
        this.headerAdapter.clear();
        this.sectionAdapters.clear();
    }

    @Override
    public int getCount() {
        // total together all sections, plus one for each section header
        int total = 0;
        for(Adapter adapter : this.sectionAdapters.values())
            total += adapter.getCount() + 1;
        return total;
    }

    @Override
    public int getViewTypeCount() {
        //count headers, then total all sections
        int total = this.headerAdapter.getViewTypeCount();
        for(Adapter adapter : this.sectionAdapters.values())
            total += adapter.getViewTypeCount();
        return total;
    }

    @Override
    public int getItemViewType(int position) {
        int type = 1;
        for(Object section : this.sectionAdapters.keySet()) {
            Adapter adapter = sectionAdapters.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) return TYPE_SECTION_HEADER;
            if(position < size) return type + adapter.getItemViewType(position - 1);

            // otherwise jump into next section
            position -= size;
            type += adapter.getViewTypeCount();

        }
        return -1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int sectionnum = 0;
        for(Object section : this.sectionAdapters.keySet()) {
            Adapter adapter = sectionAdapters.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) 
                return headerAdapter.getView(sectionnum, convertView, parent);
            if(position < size) 
                return adapter.getView(position - 1, convertView, parent);

            // otherwise jump into next section
            position -= size;
            sectionnum++;
        }
        return null;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return (getItemViewType(position) != TYPE_SECTION_HEADER);
    }
}

我遇到的问题是 API 将 onvertView 对象从错误的适配器提供给 getView(),从而导致问题。我认为我正确实现了 getViewTypeCount() 和 getItemViewType() 并使用调试器进行了检查。还有什么可能出错的地方?

这是我的适配器之一:

public static class MeetingAdapter extends ArrayAdapter<Meeting> {
    static class ViewHolder {
        TextView title;
    }
    private LayoutInflater inflater;

    public MeetingAdapter(Activity context, List<Meeting> meetings) {
        super(context, 0, meetings);
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Meeting meeting = getItem(position);

        View rowView = convertView;
        ViewHolder holder;

        // instanceof should not be necessary!!! normally convertView should be of the right type!
        if (rowView == null || !(rowView.getTag() instanceof ViewHolder)) {
            rowView = inflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.title = (TextView)rowView; // tmp - layout contains only a textview for the moment
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder)rowView.getTag();
        }
        holder.title.setText(meeting.getTrack());
        return rowView;
    }
}

请帮助?为这件事而疯狂。


我追踪问题出在列表视图的 RecybleBin 对象上。它根据 getItemViewType 报告的类型存储可能的 ConvertView,但是在更改数据集(因此位置)后,其内容永远不会更新,然后当您的适配器根据其位置获取曾经是类型“X”视图的内容时,但是现在它恰好是“Y”类型。 解决方案是手动检查 getView 是否不仅 ConvertView 为空,而且它的类型是否正确(有时这真的很尴尬......如果您使用视图持有者模式,您可以检查标签是否与相应的视图持有者类匹配)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 getView 在分离列表适配器上返回错误的 ConvertView 对象? 的相关文章

  • 如何在 Android 中使用 KSoap 2

    我刚刚发现 ksoap2 在 Android 应用程序中使用我自己的 asp net Web 服务 我在互联网上发现了一些很棒的资源 并且我已经在 Android 应用程序中实现了我的网络服务 以下是我使用的网络服务的响应 HTTP 1 1
  • 如果我在 XML 布局中声明一个片段,如何将它传递为 Bundle?

    我有一个活动已替换为片段 该活动采用了一个 Intent 其中包含一些有关该活动应该显示哪些数据的额外信息 现在 我的 Activity 只是执行相同工作的 Fragment 的包装器 如果我使用标记在 XML 中声明该片段 如何将该捆绑包
  • 当通过音频采样的数据数量超过 AudioRecord 构造函数中设置的“bufferSizeInBytes”时会发生什么?

    public AudioRecord int audioSource int sampleRateInHz int channelConfig int audioFormat int bufferSizeInBytes 这是公共构造函数Au
  • CursorAdapter 破坏了 CHOICE_MODE_MULTIPLE 选项

    我有一个ListFragment 我在其中添加一个CursorAdapter to my ListView 并且我希望能够单击几行以使用上下文操作栏 我使用 SherlockActionbar 当我使用一个简单的ArrayAdapter 但
  • Android 在连接 Socket 时出现错误

    在阅读了一些express io文档并成功连接到之后 我尝试使用nodejs和express io编写简单的应用程序http chat socket io在命令行中运行下面的代码并打开后 我找到了使用 nodejs 和express io
  • 如何在android中对Log.e进行单元测试?

    我需要执行单元测试 在应用程序中发生特定情况时 我需要检查是否记录错误消息 try do something catch ClassCastException IndexOutOfBoundsException e Log e INFOTA
  • 调用“DisplayManagerGlobal.getDisplayInfo()”会导致应用程序中的应用程序无响应 (ANR)

    显然 应用程序中的某些内容从不同线程 主线程和绑定器线程 调用该方法 这会导致内部 ANR 它经常发生 我不知道它发生在哪里 因为我无法在模拟器或我拥有的测试设备上重现它 该应用程序的作用 它是一个应用程序储物柜应用程序 它在应用程序覆盖层
  • 何时使用支持库

    我对 Android 支持库的用途和何时需要它感到困惑 据我了解 使用支持库的主要优点是 Android 可以在旧版本中自行实现主题和 UI 功能 而无需开发人员显式定义它们 这些关键的 UI 功能之一是操作栏 它是为 Honeycomb
  • 支持多屏幕[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 支持多个屏幕和不同的屏幕密度必须注意
  • DrawerLayout 第一次打开有点步骤

    我有一个 DrawerLayout 当我第一次滑动它时 它是逐步出现的 比如滞后或类似的东西 但之后它移动得很好 我不知道要发布什么代码 因为正如我所说 它工作正常 只是第一次打开它时 它打开不顺利 这是我的布局
  • 如何转到材料日历视图中选定的日期?

    我在用着材料日历视图 https github com prolificinteractive material calendarview在我的项目中 我可以使用 setSelectedDate 方法更改日期的选择 我有一个 今天选择 按钮
  • 使用 GestureDetector 时出现 NullPointerException

    下面是在发生不同事件时加载两个不同图像的帧动画的代码 第一个事件是在活动开始时 其他的是onTouch 我在哪里利用GestureDetector为了onDown and onScroll 问题是我得到NullPointerExceptio
  • JavaFX 自定义列表单元格,updateItem 被多次调用

    我正在使用一个ListView在 JavaFX 应用程序中 列表中的项目需要的不仅仅是一个字符串来显示它们 所以我做了一个自定义实现ListCell
  • 动态元素 ID 到 setId(int) - Android

    我看到了语法定义setId int and findViewByID int 但我们如何使用findViewById R id row1 我尝试使用这个 Object1 setId Integer parseInt repeat 它没有显示
  • 如何检查Android应用程序是否第一次打开

    我想在用户第一次打开应用程序时有一个弹出窗口 如何查看 获取应用程序被打开的次数 请帮忙 多谢 当您的应用程序启动时 在onCreate 方法 您可以检查 SharedPreference 是否存在 如果没有 则这是该应用程序第一次启动 然
  • android应用程序在模拟器上运行但在手机上运行

    我有我开发的这个应用程序 它在模拟器上运行得很好 没有任何错误 但当我尝试在手机上运行相同的代码进行测试时 应用程序崩溃并提示 filenotfoundexception 它说文件 res drawable divider horizo n
  • 在 Android 中的计时器内运行异步任务

    我正在开发一个基本的聊天类型应用程序 目前我正在运行代码 如下所示 class GetMsgs extends AsyncTask
  • 在 Android 应用程序中编辑/添加 IPTC 元数据

    我看过许多其他类似的问题 但似乎没有一个有准确的答案 我正在开发一个可处理大量图像的 Android 应用程序 我希望通过编辑 IPTC 关键字标签 或其他适当标签 的值来向图像添加信息 我在用元数据提取器 http drewnoakes
  • Android 2.3 或更低版本上通知中的可点击自定义视图

    我创建了一个自定义通知布局 其中有一个可单击的按钮 到目前为止 在 Android 3 API 级别 11 或更高版本上可以正常工作 但在 Android 2 3 即ContentIntent来自Notification总是覆盖我的布局并且
  • Pinterest 喜欢自定义 GridView

    我是 Android 新手 我正在寻找网格视图的逻辑 例如为 iPhone 构建的 pinterest homescreen 应用程序 一个大号 图像来自服务器 我需要以以下形式显示并具有分页效果 即在滚动上加载图像 如果可以的话请回复 我

随机推荐