SugarORM + Kotlin: Unresovled reference "listAll"

1.1k views Asked by At

I am trying to use the gorgeous Kotlin and SugarORM in combination for Android development and have my models 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 have also changed the 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'm trying to use the model inside 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 the error Unresolved reference: listAll, meaning the static method call failed for some reason. Same with methods like find… did I overlook something?

Thanks in advance for any help.

2

There are 2 answers

0
Kirill Rakhman On BEST ANSWER

As those are static methods, you need to call them on the declaring class SugarRecord. Your code should be:

SugarRecord.listAll(Contact::class.java)
1
heyarne On

In lack of a better explanation, this seems to be a Kotlin bug. It seems like the static methods are not inherited. Creating a helper class like this:

public class ModelHelper {    
    public static List<Contact> getAllContacts() {
        return Contact.listAll(Contact.class);
    }
}

and calling this from the Kotlin code like

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    val contacts = ModelHelper.getAllContacts()    
    return true
}

works.