Kotlin with JavaFX - ListView css class applying also in empty rows

Solution 1:

I'll answer my question and take the opportunity to clarify how I solved it:

As I said, the problem was that I was regenerating the "ObservableList" that contained the customer data, instead of just updating the "active" property.

Earlier in the code, what was done was to use the generateListView() method to remove the items from the ListView and repopulate it again, each time a client was disabled or enabled. To fix it I created the updateListViewContent() method. As you can see, this method only updates the "active" property, taking the value assigned inside listCustomers.

private fun updateListViewContent(){
    listCustomers.forEach { customer ->

        val id = customer.getValue("id")
        val active = customer.getValue("active").toInt()

        lvCustomers.items.forEach { lvCustomer ->
            if(lvCustomer.getId() == id){
                lvCustomer.setActive(active)
            }
        }

    }

    objListViewWrapper.clearSelection()
}

listCustomers is a list that contains maps with the data of each customer, and lvCustomers is the ListView that contains the items of type CustomerData. If the id of the client that has been disabled or enabled matches one of those that exists in the ListView, the active property is updated.

The active property can be changed directly by clicking the buttons. This example doesn't really need listCustomers. This is a variable that I use in the original code to store extra data, which does not belong to this ListView, and which is collected in forms presented in a "step-by-step" way.