[mono-android] SimpleCursorAdapter and SimpleCursorAdapter.CursorToStringConverter

Jonathan Pryor jonp at xamarin.com
Sun Jan 15 16:14:54 UTC 2012


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



More information about the Monodroid mailing list