[mono-android] SimpleCursorAdapter and SimpleCursorAdapter.CursorToStringConverter

Tom Opgenorth tom at opgenorth.net
Sun Jan 15 19:45:45 UTC 2012


Cool.  Thanks. While there isn't much added for the cursor to string
conversion, the closure trick is handy for the FilterQueryProvider
that I also need to set on my SimpleCursorAdapter.

On Sun, Jan 15, 2012 at 09:14, Jonathan Pryor <jonp at xamarin.com> wrote:
> On Jan 14, 2012, at 11:47 PM, Tom Opgenorth wrote:
>> Is this the correct way to port this code over?  Or is there a simpler way.
>
> Yes, that's a correct way. A variation on that is to simplify your SimpleCursorAdapter.ICursorToStringConverter implementation to rely on delegates, which would allow for closure use:
>
>        class CursorToStringConverter : Java.Lang.Object,    SimpleCursorAdapter.ICursorToStringConverter {
>                Func<ICursor, ICharSequence> converter;
>
>                public CursorToStringConverter (Func<ICursor, ICharSequence> converter)
>                {
>                        this.converter = converter;
>                }
>
>                public ICharSequence ConvertToStringFormatted(ICursor cursor)
>                {
>                        return converter (cursor);
>                }
>        }
>
> This would permit your callsite to become:
>
>        adapter.CursorToStringConverter = new CursorToStringConverter (c => {
>                        var colIndex = c.GetColumnIndexOrThrow(VehicleTripDescriptionTable.Columns.DESCRIPTION);
>                        var value = c.GetString(colIndex);
>                        return new Java.Lang.String(value);
>        });
>
> In this case it might not buy you anything, but if you need multiple SimpleCursorAdapter.ICursorToStringConverter implementations then you'll need only one interface implementation and each callsite can provide a different ConvertToStringFormatted() "body", similar to what Java does.
>
>  - Jon
>
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid



-- 
http://www.opgenorth.net


More information about the Monodroid mailing list