0

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?

1
  • Can you please share 'array' list your logic is correct need to check array Commented Aug 8, 2022 at 10:12

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

Even cleaner way to do the same. Thank you for the insight
2

Try this instead

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

1 Comment

Thanks man, i completely forgot about that way. By your approach you need to change a val to a var
1

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!

Comments

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.