简体   繁体   中英

fetching data from Firebase with Android Kotlin

i want to fetch all my data from data base and i have a little probleme doing this, my code work fine for me, i get the result i want, but in fact the code isn't too correct, i tried many solution to get the parent node("Banc","Salon","Table) automatically with a loop but i didn't get the right solution, so insert them manually

        mdatabase = FirebaseDatabase.getInstance().reference
        mdatabase!!.addValueEventListener(object : ValueEventListener {

               override fun onDataChange(snapshot: DataSnapshot) {

               //  for (aaaa: DataSnapshot in snapshot.children){ i believe there's a solution
//using this loop, but i don't know how to get the right data from my firebase and send them to 
//the adapter with this order Meuble(val imageResource: Int = -1, val nom: String="", val prix: Int=-1,val stock:Int=-1)


                   val ir = snapshot.child("Banc").child("imageResource").value.toString().toInt()
                   val n = snapshot.child("Banc").child("nom").value.toString()
                   val p = snapshot.child("Banc").child("prix").value.toString().toInt()
                   val st = snapshot.child("Banc").child("stock").value.toString().toInt()

                       listMeubles.add(Meuble(ir,n,p,st))
                   val iroo = snapshot.child("Salon").child("imageResource").value.toString().toInt()
                   val noo = snapshot.child("Salon").child("nom").value.toString()
                   val poo = snapshot.child("Salon").child("prix").value.toString().toInt()
                   val stoo = snapshot.child("Salon").child("stock").value.toString().toInt()
                   listMeubles.add(Meuble(iroo,noo,poo,stoo))
                   val irii = snapshot.child("Table").child("imageResource").value.toString().toInt()
                   val nii = snapshot.child("Table").child("nom").value.toString()
                   val pii = snapshot.child("Table").child("prix").value.toString().toInt()
                   val stii = snapshot.child("Table").child("stock").value.toString().toInt()
                   listMeubles.add(Meuble(irii,nii,pii,stii))
                     
                   mon_recycler.setHasFixedSize(true)
                   mon_recycler.layoutManager = LinearLayoutManager(this@ListeMeuble3DActivity)
                   mon_recycler.adapter = MeubleAdapter(listMeubles.toTypedArray()){
                       val intent3 = Intent(this@ListeMeuble3DActivity,SceneformKot::class.java)
                       intent3.putExtra("image_url", it.nom)
                       startActivity(intent3)
                   }
               }

               override fun onCancelled(error: DatabaseError) {
                   TODO("Not yet implemented")
               }
           })

my data base look like this:

https://imgur.com/a/Sz1gSmq

EDIT: my data class:

data class Meuble(val imageResource: Int = -1, val nom: String="", val prix: Int=-1,val stock:Int=-1)

You should make a class let's say "Product" with the variables nom, prix, stock etc. Then try to get the values in them. Do not forget to make getters and setters and constructor for the class.

And if you are displaying these values somewhere then it is a very good idea to use RecyclerView to get the values, I do not know how you are using the RecyclerView here. Firebase has an adapter for it and it can populate the RecyclerView for you automatically.

I am sorry I cannot help with code as I dont know Kotlin, but I hope you understood where you can go from here.

So adding a listener just on raw ref will return you DB root. You can use mdatabase...addChildEventListener(...) to get all root children Or you can with your current mdatabase...addValueEventListener(...) get all children iterable with: snapshot.children

I have not been working with Firebase for a while, but here is what I would suggest: (Please note code might require some adjustments)

You can have your data mapped instead of reading field by field:

Create a user class:

@IgnoreExtraProperties
data class UserInfo(
    var imageResource : Long = 0,
    var nom: String? = null,
    var prix: Int = 0,
    var stock: Int = 0,
)


mdatabase!!.addValueEventListener(object : ValueEventListener {
     override fun onDataChange(dataSnapshot: DataSnapshot) {
          if (dataSnapshot.exists() && dataSnapshot.hasChildren()) {
                dataSnapshot.children.map {it.getValue(UserInfo::class.java)!! }
            }
     }

     override fun onCancelled(error: DatabaseError) {
           TODO("Not yet implemented")
     }
 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM