Created
December 25, 2016 20:23
-
-
Save tkshnwesper/292748b3e3b0ffc65110f132b7b38d39 to your computer and use it in GitHub Desktop.
An example of how to use doAsyncResult in Anko
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
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