Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java constants from Scala in Android

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.ContactsContract
[error]     ContactsColumns.LOOKUP_KEY,
[error]     ^
[error] one error found
like image 810
exine Avatar asked Jul 08 '11 18:07

exine


1 Answers

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.

like image 199
Daniel C. Sobral Avatar answered Oct 16 '22 01:10

Daniel C. Sobral