skip to Main Content

I’m new to android development and im having a bit of problem with firebase database. I’m creating a app somewhat similar to e-commerce application.

category1
    child1
         name: nameOfChild1
         value: value
    child2
         name: nameOfChild2
         value: value
    child3

This is how my database is structured. im using

dbref = FirebaseDatabase.getInstance().getReference("requiredCategory")
        dbref.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                if (snapshot.exists()) {
                    for (productSnapshot in snapshot.children) {
                        val product = productSnapshot.getValue(Product::class.java)
                        productlist.add(product!!)
                    }
                    productrecyclerview.adapter = productAdapter(productlist)
                }
            }

And Product class is

data class Product(
    var id: String? = null,
    var price: String? = null,
)

Instead i would like to change my structure to

category1
    child1
      nameOfNested1: value
      nameOfNested2: value
    child2
      nameOfNested1: value
      nameOfNested2: value
    child3
      nameOfNested1: Value
      nameOfNested2: value

category2
    child1
    child2
    child3

I want to retrive both the key: nameOfNested and value:value .How do i go on and change the code get both the id and value?
Thanks in Advance

2

Answers


  1. Chosen as BEST ANSWER

    Let me answer with what i got working and also be a bit more specific about what i was trying to accomplish

    First of all, let me start with what i was doing. i had a Firebase Realtime Database that looked like this (Only an example for demonstration purpose)

    Students
       Riya
         name: Riya 
         class: 10
       Benson
         name: Benson
         class: 9
       Merlin
         name: Merlin
         class: 7
    

    I Made a student class that looks like this

    data class Student(
        var name: String? = null,
        var class: String? = null,
    )
    

    I found a snippet online that would retrieve data from db that is given below

    dbref = FirebaseDatabase.getInstance().getReference("Students")
            dbref.addValueEventListener(object : ValueEventListener {
                override fun onDataChange(snapshot: DataSnapshot) {
                    if (snapshot.exists()) {
                        for (studentSnapshot in snapshot.children) {
                            val student = studentSnapshot.getValue(Student::class.java)
                           studentlist.add(student!!)
                        }
                        myrecyclerview.adapter = thisAdapter(studentlist)
                    }
                }
    

    It gave a Array that look something like this

    [Student(name=Riya, class=10), Student(name=Benson, class=9), Student(name=Merlin, class=7)]
    

    As you can see, the structure that i was using was inefficient and i want to change it. Also, instead of a single Students refence i wanted to add child which holds the students based on the class they are in. Also i wanted to add age property to each student as well. So i came up with a structure that looks like this

    Students
       Class7
          Joe: 12
          Mary: 11
          Ann: 12
       Class8
          Michael: 12
          Lucy: 12
       Class9
          Ricko: 15
          Anna: 14
    
    Staff
        class7
          Janet:25
          Bob: 34
    
    

    What i was having issue with getting Both the Key and value at the same time. what at the time i was using was

    FirebaseDatabase.getInstance()
                .getReference("Students")
                .child(requiredClass)
                .addValueEventListener(object : ValueEventListener {
                    override fun onDataChange(snapshot: DataSnapshot) {
                        if (snapshot.exists()) {
                            for (studentSnapshot in snapshot.children) {
                         val student = studentSnapshot.getValue(Student::class.java)
                                studentlist.add(student!!)
                            }
    

    It was not working with what i had.. It was looking for ids name and age. (i renamed the class to age in the Student class)

    I got it working by doing something like this

    FirebaseDatabase.getInstance()
                .getReference("Students")
                .child(requiredClass)
                .addValueEventListener(object : ValueEventListener {
                    override fun onDataChange(snapshot: DataSnapshot) {
                        if (snapshot.exists()) {
                            for (studentSnapshot in snapshot.children) {
    
                                val student = Student()
                                student.name = studentSnapshot.key.toString()
                                student.age = studentSnapshot.value.toString()
                                studentlist.add(student!!)
                            }
    

    This works fine and returns a array that i wanted.

    I know most of that explanation was unnecessary. i wanted to clarify what my situation was. Also my solution might be the worst. If anyone have a better solution, i'm all ears...


  2. If you also want to get the key of each product, you can do so with:

    for (productSnapshot in snapshot.children) {
        val key = productSnapshot.key // 👈
        val product = productSnapshot.getValue(Product::class.java)
        ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search