1

I've created a HashMap in Kotlin, putting in the custom class types as the keys and the values. The HashMap is being stored in the presenter and updated by selecting and unselecting items from the adapter.

It all works well until items in the adapter have been updated due to an API call. At that point, I am no longer able to remove items from the HashMap.

The case is next: I select a couple of items and then update their attribute by making an API call. Adapter list is being refreshed and then, when I try to unselect an item, the change is not applied (item does not remove) in the HashMap in the presenter.

That's why I am finding the HashMap Key by ID, which does not change at any time! The strange thing is that I am able to find the key using the Kotlin's find statement, but when I check if the HashMap contains the specific key, the result is false.

*Note: addition and removal of items in the HashMap works well before updating the items in the adapter. Also, I solved the problem with a different approach, but I am interested in the background of it so if anyone has a clue what's going on here, bring it on! :-)

private val myHashMap: HashMap<Class1, Class2>) = hashMapOf()
private val value: Class2 = Class2()

override fun updateHashMap(key: Class1, isAdded: Boolean) {
        if (isAdded) myHashMap[key] = value
        else {
            // Finds the correct item.
            val mKey = myHashMap.keys.find { it.id == key.id }
            // Logs false.
            Log.d("Does the key exist?", "${myHashMap.contains(mKey)}")

            // The removal should be applied..
        }
}
Luja93
  • 201
  • 2
  • 5

0 Answers0