Skip to content

Instantly share code, notes, and snippets.

@tkshnwesper
Created December 25, 2016 20:23
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 tkshnwesper/292748b3e3b0ffc65110f132b7b38d39 to your computer and use it in GitHub Desktop.
Save tkshnwesper/292748b3e3b0ffc65110f132b7b38d39 to your computer and use it in GitHub Desktop.
An example of how to use doAsyncResult in Anko
class PopularActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
listView {
id = ViewID.ID_POPULAR_LIST
// This task will happen asynchronously in a separate thread (so you can perform network operations)
// and the returned value can be accessed through the Future object
// f.get() blocks till the task is finished
fun getTitles() : ArrayList<String> {
val arr = ArrayList<String>()
// add elements to list
return arr
}
// so yeah, this is the tricky part (for me at least) since it wasn't updated in the documentation. Enjoy! :)
val m: (AnkoAsyncContext<ListView>.() -> ArrayList<String>) = {
::getTitles.invoke()
}
val f : Future<ArrayList<String>> = doAsyncResult(null, m)
adapter = ArrayAdapter<String>(ctx, android.R.layout.simple_list_item_1, f.get())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment