开发者

Using Java constants from Scala in Android

开发者 https://www.devze.com 2023-03-18 20:21 出处:网络
I have a strange problem with developing in Scala on Android. I\'m using sbt android plugin and right now I\'m trying to get content providers working, but...

I have a strange problem with developing in Scala on Android. I'm using sbt android plugin and right now I'm trying to get content providers working, but...

I need to get array of columns, and I'm doing it exactly like in tutorial here: http://developer.android.com/guide/topics/providers/content-providers.html

Just replaced Java's array code with Scala one. It looks like this:

val projection = Array(
  People.NAME,
  People.NUMBER
)

But then, scala compiler returns error

[error] /home/exine/proj/hello-world/src/main/scala/Activity.scala:12: value NAME is not a member of object android.provider.Contacts.People
[error]     People.NAME,
[error]            ^
[error] one error found

And actually it is member of this object, and it's defined in interface android.provider.Contacts.PeopleColumns (which is implemented by People). I've tried getting it directly from PeopleColumns too without any success (same error). Is getting constants from Java in Scala somewhat different or I just made a stupid mistake somewhere?

UPDATE:

Next problem. When using ContactsContract.Contacts error stays the same, and when trying another method I can't access ContactsContract.ContactsColumns directly because it's protected interface.

[error] /home/exine/proj/hello-world/src/main/scala/Activity.scala:13: object ContactsColumns cannot be accessed in object android.provider.Co开发者_JAVA百科ntactsContract
[error]     ContactsColumns.LOOKUP_KEY,
[error]     ^
[error] one error found


This context is deprecated according to the reference. The new way is ContactsContract, and you should use that.

Anyway, the problem is that this constant is a static defined on the interface PeopleColumns. Inheritance of statics is something that Java has but Scala doesn't, and it's frowned upon even in Java.

Try importing PeopleColumns and using PeopleColumns.NAME instead -- or, better yet, use the new, non-deprecated, way of doing things. Which I don't explain because I have no knowledge of Android development.


I think you’ll have to import static variables with android.provider.Contacts.PeopleColumns._ before you can access them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号