zulu_papa
zulu_papa

Reputation: 415

How to check if boolean property of an object in array is true with Kotlin?

I am trying to validate an array in Android app written with Kotlin. It is an array of objects. This is the code that always returns 0. This might be even some deeper problem, but for now i am looking for any other way to get the count right.

private fun count(array: Array<Item>): Int {
    val selectedItemCount = 0
    array.forEach { item ->
        if (item.isSelected) selectedItemCount + 1
    }
    return selectedItemCount
}

Basically my problem is that if the count is 0 and none items are selected i want to display no items selected message, otherwise navigate to next screen. I believe that i got this part right. When i log the count every time it returns 0 although the items selected are true within array.

Any help please?

Upvotes: 0

Views: 1561

Answers (3)

cactustictacs
cactustictacs

Reputation: 19524

Just to show a couple more tricks...

You could make an extension function like this:

// you can do count { it.isSelected } but sometimes it's nice to explicitly
// reference the property and the type like this
fun Array<Item>.countSelected() = count(Item::isSelected)

and call it with array.countSelected(). But it also feels a bit like a property of the array, right? How many are selected. So instead of calling a function, you could make it an extension property instead:

val Array<Item>.selectedCount get() = count(Item::isSelected)

And now you can just reference array.selectedCount like it's any other property on an object. Under the hood it does a calculation when you access it (it's a get() function really!) but it can just read nicer than a function call:

if (array.selectedCount == 0) displayNoItemsSelected()

I mean if you don't even care about the count, just whether something is selected, you could write an extension for that instead!

val Array<Item> noneSelected get() = count(Item::isSelected) == 0
// or (more efficient since it'll return early if one is selected)
val Array<Item> noneSelected get() = none(Item::isSelected)
// or if you have the other property too and want to reuse it
val Array<Item> noneSelected get() = selectedCount == 0

if (array.noneSelected) displayNoItemsSelected()

etc!

Upvotes: 1

dey
dey

Reputation: 3140

You can improve the code by just using the count with predicate function to do the same in a cleaner way (in this example, the function counts all elements having isSelected set to true):

private fun count(array: Array<Item>): Int {
    return array.count { it.isSelected }
}

There are a few problems in the original question:

  • in the first line, you create val (which is final type, so you can't change it's value). You can use var instead
  • this operation: selectedItemCount + 1 adds 1 to selectedItemCount and returns it's value (it's not modifying the input variable). You can use selectedItemCount += 1 operator instead (add and update the variable), or simply selectedItemCount++ if you just want to increment by one

Upvotes: 7

Khanh Tran
Khanh Tran

Reputation: 73

Try this instead

array.forEach { item ->
        if (item.isSelected) selectedItemCount += 1
    }

Upvotes: 2

Related Questions