Home > front end >  Access next element while looping a list of edittexts
Access next element while looping a list of edittexts

Time:01-14

I have a list of edit texts. I loop through them and check if they have text in them, if so I make them visible. There is one cavet, I also need the next edit text in the list to display even if it has no text in it. Is it possible to access the next element in the loop and make it visible?

For example if I have 5 edit texts and et 1 -> 3 have text in them the loop below will make them visible. But I also need et 4 to display, but not et 5. Just the one after when the text stops.

       lifecycleScope.launch(Dispatchers.Default) {
                val etList = createEditTextList()

       etList.forEach { item ->
                    withContext(Dispatchers.Main) {
                        if (!item.text.isNullOrEmpty()) {
                            item.visibility = View.VISIBLE
                        }
                    }
                }



   private fun createEditTextList(): List<EditText> {
    with(binding) {
        return listOf(
            etChallenges1,
            etChallenges2,
            etChallenges3,
            etChallenges4,
            etChallenges5,
            etChallenges6,
            etChallenges7,
            etChallenges8,
            etChallenges9,
            etChallenges10
        )
    }
}

CodePudding user response:

First, I recommend you avoid calling blocking functions in your top-level coroutine. If you make createEditTextList() into a suspend function, like this:

private suspend fun createEditTextList() = withContext(Dispatchers.Default) {
    //...
}

Then your above code can be simplified to:

lifecycleScope.launch {
    val etList = createEditTextList()

    etList.forEach { item ->
        if (!item.text.isNullOrEmpty()) {
            item.visibility = View.VISIBLE
        }
    }
}

To be able to handle the next item, use a traditional for loop on the indices:

lifecycleScope.launch {
    val etList = createEditTextList()

    for (index in etList.indices) { 
        val item = etList[index]
        if (!item.text.isNullOrEmpty()) {
            item.visibility = View.VISIBLE
            if (index   1 < etList.size) {
                etList[index   1].visibility = View.VISIBLE
            }
        }
    }
}

CodePudding user response:

I think that the way you do your coroutines and the actual object you are working on are besides the point of your issue, they are extra info.

It is easier to explain what you need if we just assume a simple object like:

data class MyClass(
    val text: String,
    var visible: Boolean
)

Since we have a list, we can simply use windowed. This will group the elements in any form you want and take any steps you want. So something simple like this should work:

val list = listOf(
    MyClass("a", false),
    MyClass("b", false),
    MyClass("", false),
    MyClass("", false),
    MyClass("e", false)
)

list.windowed(size = 2, step = 1, partialWindows = true)
    .forEach { window: List<MyClass> ->
        val first = window.first()
        val second = window.last()

        if (first.text.isNotBlank()) {
            first.visible = true
            second.visible = true
        }
    }

println(list)

The result will be:

[
MyClass(text=a, visible=true), 
MyClass(text=b, visible=true), 
MyClass(text=, visible=true), 
MyClass(text=, visible=false), 
MyClass(text=e, visible=true)
]

We need to set partialWindows to true, because otherwise if we have an odd number of elements, then the last element will not be a "first" in any group.

  •  Tags:  
  • Related