Reputation: 11
I'm trying to create an Android App which uses Android Room as it's Database. I have linked two tables via their ids. If i check, whether the parent should be destroyed (because there is no child left), I come across an error with an ArrayList, that contains those child ids. I can't remove the id integer from the ArrayList of the parent.
IdList.contains(childId) returns false. Logging all integers in the IdList shows that the childId is in the IdList.
IdList.indexOf(childId) returns the right value. trying IdList.removeAt(IdList.indexOf(childId)) throws an Error tho: java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
IdList.remove(childId) does nothing at all (childId can't be found).
entryModel.entry.value?.let { meal ->
val parentDao = db.mealParentDao()
val parent = parentDao.findById(meal.childParentId)
parent?.let {
if (parent.childrenIds != null &&parent.childrenIds!!.size > 1) {
var location: Int? = null
parent.childrenIds?.forEachIndexed { index, i ->
if (i == meal.childId) location = index
}
if (location != null) parent.childrenIds?.removeAt(location!!)
parent.parentType?.remove(meal.childType)
parentDao.updateMealSpecies(parent)
} else parentDao.deleteById(parent.speciesId)
}
db.mealChildDao().deleteById(meal.childId)
}
I'd expect the ArrayList without the id of the deleted child, but the ArrayList either stays the same or brings my App to a Crash.
Thanks in advance for all your help!
Upvotes: 0
Views: 67
Reputation: 24
I'm not sure if this is what's going on here, but there's a well known problem with ArrayList.remove()
when dealing with integers.
The remove()
method is overloaded. It can either be called with an object to remove that object, or an integer index to remove the element at that index. When remove()
is passed an integer, even if that integer happens to be an element of the list, it presumes it to be an index.
Is it possible that that's the issue?
Upvotes: 1