I try to learn Kotlin by converting my java implementation to kotlin. Currently I stuck at the following error message from Kotlin "unresolved reference: removeAt"
Here is my Kotlin code:
private val mActivityTaskMap = mutableMapOf<String, List<CustomAsyncTask<*, *, *>>>()
fun removeTask(task: CustomAsyncTask<*, *, *>) {
for ((key, tasks) in mActivityTaskMap) {
for (i in tasks.indices) {
if (tasks[i] === task) {
tasks.removeAt(i) // <==== ERROR
break
}
}
if (tasks.size == 0) {
mActivityTaskMap.remove(key)
return
}
}
}
And here the original java implementation:
private Map<String, List<CustomAsyncTask<?,?,?>>> mActivityTaskMap;
public void removeTask(CustomAsyncTask<?,?,?> task) {
for (Map.Entry<String, List<CustomAsyncTask<?,?,?>>> entry : mActivityTaskMap.entrySet()) {
List<CustomAsyncTask<?,?,?>> tasks = entry.getValue();
for (int i = 0; i < tasks.size(); i++) {
if (tasks.get(i) == task) {
tasks.remove(i);
break;
}
}
if (tasks.size() == 0) {
mActivityTaskMap.remove(entry.getKey());
return;
}
}
}
How can I remove the specific task? Must I change the declaration to:
private val mActivityTaskMap = mutableMapOf<String, MutableList<CustomAsyncTask<*, *, *>>>()
The list you're trying to edit is immutable inside the map. Make it mutable:
private Map<String, MutableList<CustomAsyncTask<?,?,?>>> mActivityTaskMap;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With