Home > OS >  Create and get array in a Firebase Realtime Database
Create and get array in a Firebase Realtime Database

Time:12-05

I have just linked and started storing values in my realtime database and it all works smoothly. I have noticed however that when attempting to set another string to the list its overwrite it instead of having multiple values. My goal is to store multiple numbers (stored as strings) to the branch so I can calculate an average value to display in my app.

Here is current code:

val myRef = database.getReference("/general/$position") //sets location to save data, (In the section general, subcategory being the number of the list item being used

//set up button that is in the dialogue
val button = dialog.findViewById<View>(R.id.menu_rate) as Button
button.setOnClickListener {

    val simpleRatingBar =
        dialog.findViewById<View>(R.id.ratingBar) as RatingBar // initiates the rating bar

    val ratingNumber = simpleRatingBar.rating // get rating number from a rating bar

    Toast.makeText(this, "You rated this $ratingNumber stars!", Toast.LENGTH_SHORT).show()

    val rating = ratingNumber.toString() //converts the float to string for storage
    myRef.setValue(rating) //saves value to server
        .addOnSuccessListener {
            Toast.makeText(this, "success", Toast.LENGTH_SHORT).show()
        }

}

It stores the values correctly for each category, it just only allows for 1 value to be stored per position.

So how could I get multiple values to be stored so that I can then get the values and utilize them in my app?

CodePudding user response:

To store multiple values in a list in a Realtime Database, you can use the push() method to add a new value to the list. This method will automatically generate a unique key for the new value and append it to the list.

For example, if you want to store multiple ratings for each item in your list, you can modify your code as follows:

val myRef = database.getReference("/general/$position") // sets location to save data, (In the section general, subcategory being the number of the list item being used

// set up button that is in the dialogue
val button = dialog.findViewById<View>(R.id.menu_rate) as Button
button.setOnClickListener {

Copy code
val simpleRatingBar = dialog.findViewById<View>(R.id.ratingBar) as RatingBar // initiates the rating bar

val ratingNumber = simpleRatingBar.rating // get rating number from a rating bar

Toast.makeText(this, "You rated this $ratingNumber stars!", Toast.LENGTH_SHORT).show()

val rating = ratingNumber.toString() // converts the float to string for storage

// use the push() method to add the new rating to the list
myRef.push().setValue(rating)
    .addOnSuccessListener {
        Toast.makeText(this, "success", Toast.LENGTH_SHORT).show()
    }
}

With this code, each time the button is clicked, a new rating will be added to the list under the specified position. You can then retrieve the list of ratings using the value event listener, and calculate the average rating using the values in the list.

For more information about using lists in the Realtime Database, see the Realtime Database documentation.

  • Related