Created
January 22, 2014 15:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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