0

我想扩展 SimpleCursorAdapter,为方法添加} else if (c instanceof MyView) { setMyViewMyThing(MyView v, text)一个bindView

public void bindView(View view, Context context, Cursor cursor) {
    final ViewBinder binder = mViewBinder;
    final int count = mTo.length;
    final int[] from = mFrom;
    final int[] to = mTo;

    for (int i = 0; i < count; i++) {
        final View v = view.findViewById(to[i]);
        if (v != null) {
            boolean bound = false;
            if (binder != null) {
                bound = binder.setViewValue(v, cursor, from[i]);
            }

            if (!bound) {
                String text = cursor.getString(from[i]);
                if (text == null) {
                    text = "";
                }

                if (v instanceof TextView) {
                    setViewText((TextView) v, text);
                } else if (v instanceof ImageView) {
                    setViewImage((ImageView) v, text);
                } else {
                    throw new IllegalStateException(v.getClass().getName() + " is not a " +
                            " view that can be bounds by this SimpleCursorAdapter");
                }
            }
        }
    }
}

和是 SimpleCursorAdpater 的私有成员这一事实阻止了我这样mViewBinder做。mTomFrom

有没有比将SimpleCursorAdapter批发源复制到 aMyCursorView并将我的条件添加到更好的方法来实现我的目标bindView?(该解决方案会奏效,而且会很快,但有不良做法的味道。)

(在元级别上,Android 作者似乎已经用我扩展 SimpleCursorAdapter 的自由来换取他们更改实现的自由。有没有可以同时保留两者的语言?)

4

2 回答 2

3

如何扩展 Android 的 SimpleCursorView?

Android中没有SimpleCursorView。对于这个答案的其余部分,我假设您指的是SimpleCursorAdapter.

我想扩展 SimpleCursorView,在 bindView 方法中添加一个 } else if (c instanceof MyView) { setMyViewMyThing(MyView v, text)。

其他人都会setViewBinder()自己ViewBinder. 引用文档ViewBinder

您应该使用此类将 Cursor 中的值绑定到 SimpleCursorAdapter 不直接支持的视图,或者更改 SimpleCursorAdapter 支持的视图的绑定方式。

当然,您不必这样做。

有没有比将 SimpleCursorView 批发源复制到 MyCursorView 并将我的条件添加到 bindView 更好的方法来实现我的目标?

除了使用ViewBinder? 可能不是。

在元级别上,Android 作者似乎已经用我扩展 SimpleCursorView 的自由来换取他们更改实现的自由。

“在元级别上”,看起来 Android 作者更喜欢组合而不是继承

于 2013-10-02T13:58:56.690 回答
1

您想要实现的目标应该可以通过SimpleCursorAdapter.ViewBinder实现。实现其中之一,false如果setViewValue(View view, Cursor cursor, int columnIndex)不是view您的MyView. 然后,调用SimpleCursorAdapter.setViewBinder(SimpleCursorAdapter.ViewBinder viewBinder),传递你的ViewBinderas 参数。根据文档(以及您发布的实现),如果ViewBinder无法处理View类型(即它返回false),bindView()将回退到默认实现。

于 2013-10-02T13:58:38.343 回答