SugarORM + Kotlin:未经参考的参考“listAll”

时间:2015-06-18 15:53:26

标签: android android-studio kotlin sugarorm

我正在尝试将华丽的Kotlin和SugarORM结合使用以进行Android开发,并让我的模型设置如下:

import com.orm.SugarRecord

public class Contact : SugarRecord<Contact>() {
    var name : String = ""
    var phoneNumber : String = ""
    var info : String? = null
}

当然我也改变了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>

现在我正在尝试使用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
}

但是得到错误Unresolved reference: listAll,意味着静态方法调用由于某种原因失败了。与find等方法相同......我忽视了什么吗?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:8)

由于这些是静态方法,您需要在声明类SugarRecord上调用它们。你的代码应该是:

SugarRecord.listAll(Contact::class.java)

答案 1 :(得分:0)

由于缺乏更好的解释,这似乎是一个Kotlin错误。似乎静态方法不是继承的。创建一个这样的辅助类:

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

并从Kotlin代码中调用它,如

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

作品。