如何在kotlin上从firebase数据库中同时获得键和值



我是新的android开发和我有一点问题与firebase数据库。我正在创建一个类似于电子商务应用程序的应用程序。

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

这就是我的数据库的结构。我使用

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

Product类为

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

相反,我想把结构改成

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

我想检索key: nameOfNestedvalue:value。我如何继续并更改代码获得id和值?提前感谢

如果您还想获得每个产品的密钥,您可以这样做:

for (productSnapshot in snapshot.children) {
val key = productSnapshot.key // 👈
val product = productSnapshot.getValue(Product::class.java)
...

让我用我得到的工作来回答,也更具体一点,我想完成什么

首先,让我从我正在做的事情开始。我有一个Firebase实时数据库是这样的(Only an example for demonstration purpose)

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

我创建了一个学生类,看起来像这样

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

我在网上找到了一个片段,它将从db中检索数据,如下所示

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

它给出了一个像这样的数组

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

正如你所看到的,我使用的结构是低效的,我想改变它。此外,而不是一个单一的Students引用,我想添加一个孩子,根据他们所处的班级容纳学生。此外,我还想为每个学生添加age属性。所以我想出了这样一个结构

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

我在同时获得键和值时遇到的问题。当时我使用的是

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

它不工作与我有什么…它正在寻找idnameage。(我在Student类中将class重命名为age)

我做了这样的事情使它工作

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

返回我想要的数组。

我知道那个解释大部分是不必要的。我想澄清一下我的情况。而且我的解决方案可能是最糟糕的。如果有人有更好的解决办法,我洗耳恭听。

最新更新