Xamarin android AsyncTaskLoad class

271 views Asked by At

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.

1

There are 1 answers

2
SushiHangover On

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