1

I'm going through Big Nerd Ranch's Android Programming book and i'm currently on the chapter that teaches loaders. Also i'm not using android studio and Java, but rather Xamarin/mono for building the apps. In the book it wants me to create a class as such

public abstract class SQLiteCursorLoader extends AsyncTaskLoader<Cursor>

To translate it to C# Xamarin Android it should just be

public abstract class SQLiteCursorLoader : AsyncTaskLoader<ICursor>

However there is not a generified class of AsyncTaskLoader in Xamarin Android. Is this a bug and/or oversight? Or did they create their own class/interface that should be used? I tried IAsyncTaskLoader use the keyboard shortcut in visual studio to try and find the package to import but it didn't find anything.

Benji
  • 489
  • 2
  • 4
  • 14

1 Answers1

1

Remember you are coding in C# and Java generics are...well...Java ;-)

Xamarin.Android does supply support for some Java generics in the framework (there is a AsyncTask<TParams, TProgress, TResult> supported among others).

You should read the Generic C# classes section under the Xamarin.Android limitation guide. Also how Xamarin.Android generates callable wrappers, etc...

There is a CursorLoader class in the Android framework that is a AsyncTaskLoader<Cursor>, you could just subclass it:

public abstract class SQLiteCursorLoader : CursorLoader
{
    public SQLiteCursorLoader(System.IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }

    public SQLiteCursorLoader(Context context) : base(context) { }

    public SQLiteCursorLoader(Context context, Uri uri, string[] projection, string selection, string[] selectionArgs, string sortOrder) : base(context, uri, projection, selection, selectionArgs, sortOrder) { }
}

Or you could always code it in Java, build it into a jar/aar and bind it into a .Net library

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • I'll take a look at those hopefully tomorrow. Currently on mobile and wouldn't be able to play around with things. From my understanding, a CursorLoader won't work. The book is going through implementing it this way because the backing data store is custom sqlite tables and the normal CursorLoader doesn't support loading from those or something – Benji Sep 09 '17 at 03:23
  • @Benji In that case you can always subclass `AsyncTaskLoader` and implement the Android.Database.Cursor (`ICursor`) internally. Generics are not needed to do that. – SushiHangover Sep 09 '17 at 03:29