[SOLVED] Store edittext into MutableList

Issue

This Content is from Stack Overflow. Question asked by ottercrossing

I am trying to create a function that store 3 editext values into an object when a button is pressed. These objects are stored into an mutablelist, which will be returned and displayed in a recycleview.

    fun getWish(): MutableList<Wish>{
        val wishes : MutableList<Wish> = ArrayList()
        add_button.setOnClickListener {
            val name= name_input.getText().toString()
            val price= price_input.getText().toString()
            val url= url_input.getText().toString()
            val wishTemp = Wish(name, price, url)
            wishes.add(wishTemp)

            print(wishTemp)
            print(wishes)
        }
        return wishes
    }
  1. The issue is the list is not displaying in the recycleview. The recycle view adapter is not the issue because it was tested with a test list. Is my implementation not working?

  2. Print function shows reference values not the actual string. However, the ‘wishes’ list is added to everytime the button is pressed.



Solution

Where are you displaying the new data in the RecyclerView? All you’re doing here is creating a new list and adding some Wish objects to it. You need to add this stuff to your data set in the Adapter, and then call one of the notify* methods to tell it to update the display (like notifyDataSetChanged). You haven’t shown where you’re doing that, so there’s no way for us to know if that implementation is working or not.

(You probably want to show your adapter code anyway, there’s a difference between initialising it with some data, and updating it with some other data later)

You’re getting references with print because, at a guess, Wish is a plain class and you haven’t overridden the toString() method to make it print your strings nicely. If you can make it a data class that will give you a default implementation that prints the values of your constructor parameters.


This Question was asked in StackOverflow by ottercrossing and Answered by cactustictacs It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?