Skip to content

Instantly share code, notes, and snippets.

@tkshnwesper tkshnwesper/op.kt
Created Dec 25, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.