0

I am trying to get the properties from the list of objects and put them to the textView. I know how to get one property by the index with get(), but is it possible to retrieve all the text properties from the list and put them to the string in one line?

val listObserver = Observer<List<Word>?> { newName ->
            // Update the UI, in this case, a TextView.
            testObject.text = viewModel.listForQuiz.value?.get(5)?.text.ToString()
    }
        

here is the object class

data class Word(val lang:String,
                val text:String,
                val translations:List<Translation>){
1
  • Instead of get(), use joinToString(", ", Word::text). The first argument is whatever String you want to put between entries. Commented Oct 2, 2020 at 3:53

1 Answer 1

2

You can use joinToString()

        val listObserver = Observer<List<Word>?> { newName ->
                // Update the UI, in this case, a TextView.
                testObject.text = viewModel.listForQuiz.value?.joinToString { it.name }
        }
    
Sign up to request clarification or add additional context in comments.

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.