Skip to content

Instantly share code, notes, and snippets.

@haroldl
Created January 22, 2014 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haroldl/8560815 to your computer and use it in GitHub Desktop.
Save haroldl/8560815 to your computer and use it in GitHub Desktop.
Android Helper code to move code back and forth between an AsyncTask background thread and the main UI thread, and to implicitly convert a block of code into an OnClickListener.
object Helpers {
def backgroundThread(code: => Unit) {
val task = new AsyncTask[AnyRef,AnyRef,AnyRef]() {
override def doInBackground(params: AnyRef*) = {
code
null
}
}
task.execute()
}
def uiThread(code: => Unit) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
override def run() {
code
}
})
}
implicit def onClick(handler: View => _): OnClickListener =
new OnClickListener() {
override def onClick(source: View) { handler(source) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment