Question

I would like to extend SimpleCursorAdapter, to add an } else if (c instanceof MyView) { setMyViewMyThing(MyView v, text) to the bindView method.

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");
                }
            }
        }
    }
}

The fact that mViewBinder, mTo and mFrom are private members of SimpleCursorAdpater prevents me from doing this.

Is there a better way to achieve my objective than to copy the source of SimpleCursorAdapter wholesale into a MyCursorView and add my condition to bindView? (That solution will work, and will be quick, but reeks of bad practise.)

(On a meta-level, it looks like the Android authors have traded my freedom to extend SimpleCursorAdapter for their freedom to change the implementation. Is there any language which can preserve both?)

Was it helpful?

Solution

How to extend Android's SimpleCursorView?

There is no SimpleCursorView in Android. For the rest of this answer, I will presume that you are referring to SimpleCursorAdapter.

I would like to extend SimpleCursorView, to add an } else if (c instanceof MyView) { setMyViewMyThing(MyView v, text) to the bindView method.

Everybody else would use setViewBinder() to supply their own ViewBinder. Quoting the documentation for ViewBinder:

You should use this class to bind values from the Cursor to views that are not directly supported by SimpleCursorAdapter or to change the way binding occurs for views supported by SimpleCursorAdapter.

Of course, you do not have to do this.

Is there a better way to achieve my objective than to copy the source of SimpleCursorView wholesale into a MyCursorView and add my condition to bindView?

Besides using ViewBinder? Probably not.

On a meta-level, it looks like the Android authors have traded my freedom to extend SimpleCursorView for their freedom to change the implementation.

"On a meta-level", it looks like the Android authors favored composition over inheritance.

OTHER TIPS

What you're trying to achieve should be feasible with a SimpleCursorAdapter.ViewBinder. Implement one of these, and return false in the setViewValue(View view, Cursor cursor, int columnIndex) function if view is not an instance of your MyView. Then, call SimpleCursorAdapter.setViewBinder(SimpleCursorAdapter.ViewBinder viewBinder), passing your ViewBinder as parameter. According to the documentation (and the implementation you posted), if the ViewBinder cannot handle the View type (i.e. it returns false), bindView() will fall back to the default implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top