I have a problem with AsyncTask in Kotlin , I'm actually new so please be cool :)
So the problem here , is that I would like to use the result value which is in onPostExecute(), in another class
Let me show you my code, Here is ProviderAsync() class in my Provider.kt file ( I just create an array of Hashmap which I want to use) :
class ProviderAsync() : AsyncTask<HashMap<String, Any>, Void, ArrayList<HashMap<String, Any>>>() {
var allThings: ArrayList<HashMap<String, Any>> = arrayListOf()
override fun doInBackground(vararg params: HashMap<String, Any>): ArrayList<HashMap<String, Any>>? {
for (i in 0..2000) {
val thing = hashMapOf("Loc" to "fr", "name" to "class", "Id" to "23", "tuto" to "fr", "price" to 44)
allThings.add(thing )
}
return null
}
override fun onPreExecute() {
super.onPreExecute()
// ...
}
override fun onPostExecute(result: ArrayList<HashMap<String, Any>>?) {
super.onPostExecute(result)
// what can I do Here
}
And now here is my getThings() method in another file where i want to use the result value to get all elements of my Arraylist :
fun getThings(context: Context) {
ProviderAsync().execute()
var values = // Here i want the RESULT send from my AsyncTask
for (i in 0..values.size) {
var myObject = convertToMyObject(values[i])
allTickets.add(myObject)
}
}
Thanks and sorry for my English