SugarORM + Kotlin: Unpublished link "listAll"
I am trying to use the great Kotlin and SugarORM combined for Android development and my models are set up like this:
import com.orm.SugarRecord
public class Contact : SugarRecord<Contact>() {
var name : String = ""
var phoneNumber : String = ""
var info : String? = null
}
Of course, I also changed AndroidManifest.xml
:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:replace="android:icon"
android:name="com.orm.SugarApp">
<meta-data android:name="DATABASE" android:value="database.db" />
<meta-data android:name="VERSION" android:value="1" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="/* same as package attribute on manifest element */" />
<activity>β¦</activity>
</application>
Now I am trying to use the model internally MainActivity.kt
:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val contacts = Contact.listAll(javaClass(Contact))
// or val contacts : List<Contact> = Contact.listAll(javaClass(Contact))
return true
}
But getting an error Unresolved reference: listAll
, which means that the call to the static method failed for some reason. Same thing with methods like find
... am I missing something?
Thanks in advance for your help.
source to share
In the absence of a better explanation, this appears to be Kotlin's fault. It looks like static methods are not inherited. Create a helper class like this:
public class ModelHelper {
public static List<Contact> getAllContacts() {
return Contact.listAll(Contact.class);
}
}
and calling it Kotlin code like
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val contacts = ModelHelper.getAllContacts()
return true
}
work.
source to share