android - 为什么 CursorAdapter 与 BaseAdapter 不同?

标签 android baseadapter android-cursoradapter

我想问一下为什么 CursorAdapter 将创建 View 和用数据填充它的过程拆分为 newView()bindView()BaseAdapter 仅通过 getView() 执行此操作?

最佳答案

来自 CursorAdapter.java 的源代码, CursorAdapter 扩展了 BaseAdapter
并且可以看到getView()函数实现:

public View getView(int position, View convertView, ViewGroup parent) {
        if (!mDataValid) {
            throw new IllegalStateException("this should only be called when the cursor is valid");
        }
        if (!mCursor.moveToPosition(position)) {
            throw new IllegalStateException("couldn't move cursor to position " + position);
        }
        View v;
        if (convertView == null) {
            v = newView(mContext, mCursor, parent);
        } else {
            v = convertView;
        }
        bindView(v, mContext, mCursor);
        return v;
    } 

它做我们通常在 getView() 中做的事情(如果 convertView 为 null,则膨胀 View ,否则重用 View ),所以它只是为了让开发人员更容易或强制用户使用 ViewHolder 模式。

PS:一些开发者在 newView() 实现中调用了 bindViews() 函数,从源代码中可以看出没有必要。

关于android - 为什么 CursorAdapter 与 BaseAdapter 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18561744/

相关文章:

android - 无法访问文件 :///android_asset 中的文件

android - 如何准确地在按钮上显示 toast

android - 如何检查是否启用了定位服务?

Android Listview 项目重复添加具有不同数据的新项目

java - 自定义 ListView 中 LayoutInflater 中的空指针异常

android - 如何正确删除带有滑动的 CardView? (使用 SQLite 数据库)

android - 关于RecyclerView与CursorAdapter的疑惑

java - 在android中测量不规则多边形的面积

android - 将值从 Activity 传递到数据源的问题

android - 检测适配器上的项目 View 已被破坏