views:

213

answers:

1

I've been trying to experiment with developing Android applications with Scala.

I've gotten to the point where I can get the app to compile, but there are no helper functions for things like:

button.setOnClickListener( () => {
    text.setText("test")
})

(I'm talking about the closure there)

I see lots of references to scala-android.jar, and have this file in my project, but I'm not sure what it does, or how to use it. I get the feeling it has these helper conversion functions, but I'm not sure. Running jar -tvf scala-android.jar on the file gives me this:

401 Sun Jun 06 10:06:02 MDT 2010 scala/Function0$class.class
431 Sun Jun 06 10:06:02 MDT 2010 scala/Function0.class
572 Sun Jun 06 10:06:02 MDT 2010 scala/Function1.class
282 Sun Jun 06 10:06:02 MDT 2010 scala/ScalaObject$class.class
271 Sun Jun 06 10:06:02 MDT 2010 scala/ScalaObject.class
458 Sun Jun 06 10:06:02 MDT 2010 scala/runtime/BoxedUnit.class

If this isn't what I'm looking for, is there a simple library that'll do conversions for this kind of stuff?

+3  A: 

You may create your own, I found this is very easy and lightweight in Scala.

Here is mine, just for your reference.

trait FindView
{
    this: Activity =>

    def findView [WidgetType] (id: Int) : WidgetType = {
        return findViewById (id).asInstanceOf[WidgetType]
    }

    implicit def listenerFromFunction (action: () => Any) = {
        new View.OnClickListener() {
            def onClick(v: View) {action ()}
        }
    }
}

Mix-in this into your Activity class, and you could do it just like your example.

Brian Hsu
Nice to see some knowledge of Android and Scala together around here.
Daniel