0

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()
        }

}

Here is my database, how could I get all the number values without the keys so I can do calculations with them?

Here is my database:

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

Joshu
  • 39
  • 7
  • Have you solved the issue? – Alex Mamo Dec 05 '22 at 09:18
  • @AlexMamo I have figured out the storing aspect. However I am struggling to get the list of values from the category, I get a key instead. – Joshu Dec 05 '22 at 17:20
  • I highly recommend Firestore instead of relatime for list and arrays. However, you have to know the realtime database works like object has a hashmap thus uniq key per item per object. `{ data : "value", data2 : "value"}` this correct and works fine, but you cant do `{ data : "value", data : "value"}` . share your database structure to help you more. take a look at https://firebase.google.com/docs/database/android/lists-of-data#read_and_write_lists – Mostafa Onaizan Dec 05 '22 at 21:16
  • Please show us how do you want your database to look like. – Alex Mamo Dec 06 '22 at 06:29
  • @MostafaOnaizan, I have added a image and updated the question. Thanks for the link. I shall look at it now! – Joshu Dec 06 '22 at 16:34
  • Read this https://stackoverflow.com/questions/38652007/how-to-retrieve-specific-list-of-data-from-firebase – Mostafa Onaizan Dec 06 '22 at 20:06
  • That's great! Thank you greatly for you time!! I appreciate it :) – Joshu Dec 07 '22 at 07:03

0 Answers0