简体   繁体   English

使用 Android Kotlin 从 Firebase 获取数据

[英]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我想从数据库中获取我所有的数据,但我在执行此操作时遇到了一些问题,我的代码对我来说工作正常,我得到了我想要的结果,但实际上代码不太正确,我尝试了很多解决方案来获得父节点(“Banc”,“Salon”,“Table”)自动循环但我没有得到正确的解决方案,所以手动插入它们

        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 https://imgur.com/a/Sz1gSmq

EDIT: my data class:编辑:我的数据 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.你应该制作一个 class 让我们说“产品”,其中包含变量 nom、prix、stock 等。然后尝试获取其中的值。 Do not forget to make getters and setters and constructor for the class.不要忘记为 class 创建 getter 和 setter 以及构造函数。

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.如果您在某处显示这些值,那么使用RecyclerView获取这些值是一个非常好的主意,我不知道您在这里是如何使用RecyclerView的。 Firebase has an adapter for it and it can populate the RecyclerView for you automatically. Firebase 有一个适配器,它可以自动为您填充RecyclerView

I am sorry I cannot help with code as I dont know Kotlin, but I hope you understood where you can go from here.很抱歉,我无法提供代码帮助,因为我不知道 Kotlin,但我希望您了解从这里可以从哪里拨打 go。

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您可以使用mdatabase...addChildEventListener(...)获取所有根子级或者您可以使用当前的mdatabase...addValueEventListener(...)获取所有可迭代的子级: 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)我已经有一段时间没有使用 Firebase 了,但这是我的建议:(请注意代码可能需要一些调整)

You can have your data mapped instead of reading field by field:您可以映射数据,而不是逐字段阅读:

Create a user class:创建用户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")
     }
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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