I am trying some android development with kotlin. In my case I want to overwrite: ContentProvider where I have to overwrite the function "query". "query" returns "Cursor" type. However, when I create the Cursor instance in the function with database.query I get back a "Cursor?" type. So I can only return Cursor if it is not null, but what do I do if it is null?
This is what it basically looks like:
override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor {
val cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder)
// make sure that potential listeners are getting notified
cursor?.setNotificationUri(getContext()?.getContentResolver(), uri)
if(cursor != null)
return cursor
else
// what to do here?
Any Ideas how to solve that?
Thanks, Sven
UPDATE First of all thanks for the answers.
Annotation seems not to work in my case as I can only access the compiled code and I dont get the option to annotate the source. Maybe I am missing something here.
Implementing my own cursor Seems like overkill especially if I have to do it every time that problem occurs.
So it seems my only option is to return cursor!! But I dont know how exactly to do it. My code is a bit more complicated than in my example, I missed a when statement. This is an updated version:
override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor {
val cursor = ??? //how to initialize cursor somehow?
val db = database.getWritableDatabase()
if(db != null) {
val cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder)
// make sure that potential listeners are getting notified
cursor?.setNotificationUri(getContext()?.getContentResolver(), uri)
if(cursor != null)
return cursor
}
// return what here? cursor does not exist
}
Do I really have to implement my own cursor and clutter my code with useless "throw UnsupportedExceptions"?