0

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!

1
  • does moving parent.childrenIds?.removeAt(location) into the forEachIndexed() help? your current code is only sets the location for a single index, hence only one childrenId is removed Commented Nov 3, 2019 at 15:22

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think so. Kotlin has to functions remove() and removeAt(). Remove() uses an element, removeAt() an index. But i'll try to convert the list to long and see what will happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.