【发布时间】:2020-04-10 00:25:26
【问题描述】:
我在这里从 firebase 数据库中获取所有 "items" 条目。
实际上需要它来只给我来自特定unique key的条目
ref = FirebaseDatabase.getInstance().getReference().child("Sales")
ref.addValueEventListener(object : ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
ref.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p1: DataSnapshot) {
println("onDataChange")
if (p0.exists()) {
list.clear()
for (h in p0.children) {
if (h.hasChild("items")) {
val generic: GenericTypeIndicator<ArrayList<Product>> = object : GenericTypeIndicator<ArrayList<Product>>() {}
val items = h.child("items").getValue(generic)
items?.let { productList.addAll(it) }
}
val adapter = ReceiptItemsAdapter(applicationContext, productList)
rReceyclerView.adapter = adapter
}
}
}
})
}
})
我已经尝试过这样但它没有工作,也没有给出任何错误。
ref = FirebaseDatabase.getInstance().getReference().child("Sales/-Lw37uyfuBA0AfTalp")
这是我的 json 文件
{
"-Lw37uyfuBA0AfTalp": {
"date": "14\/12\/2019",
"discount": 0.24,
"due": 0,
"items": [
{
"pamount": 2.4,
"pname": "Granola",
"pprice": 2.4,
"punit": 1
}
],
"paym": [
{
"paymentAmnt": 0.02,
"paymentDesc": "Cash Payment :"
},
{
"paymentAmnt": 2.14,
"paymentDesc": "Card Payment :"
}
],
"subTotal": 2.4,
"time": "11:16:42",
"total": 2.16
},
"-Lw5Qx8sVdw3ZCMiXAL-": {
"date": "14\/12\/2019",
"discount": 1,
"due": 0,
"items": [
{
"pamount": 4.8,
"pname": "Granola",
"pprice": 2.4,
"punit": 2
},
{
"pamount": 2.6,
"pname": "Brownie",
"pprice": 2.6,
"punit": 1
},
{
"pamount": 2.6,
"pname": "Brownie",
"pprice": 2.6,
"punit": 1
}
],
"name": "James Brown",
"paym": [
{
"paymentAmnt": 3,
"paymentDesc": "Cash Payment :"
},
{
"paymentAmnt": 5,
"paymentDesc": "Staff Markout :"
},
{
"paymentAmnt": 1,
"paymentDesc": "Card Payment :"
}
],
"subTotal": 10,
"time": "22:03:07",
"total": 9
},
"-Lw5T4TfYUTXsWKAndBx": {
"date": "14\/12\/2019",
"discount": 0.24,
"due": -0.84,
"items": [
{
"pamount": 2.4,
"pname": "Granola",
"pprice": 2.4,
"punit": 1
}
],
"name": "James Brown",
"paym": [
{
"paymentAmnt": 3,
"paymentDesc": "Cash Payment :"
}
],
"subTotal": 2.4,
"time": "22:12:26",
"total": 2.16
},
"-Lw5UWguHS18IwWE6elT": {
"date": "14\/12\/2019",
"discount": 0.26,
"due": -1.66,
"items": [
{
"pamount": 2.6,
"pname": "Brownie",
"pprice": 2.6,
"punit": 1
}
],
"name": "James Brown",
"paym": [
{
"paymentAmnt": 4,
"paymentDesc": "Cash Payment :"
}
],
"subTotal": 2.6,
"time": "22:18:43",
"total": 2.34
},
"-Lw5ypp5xJLJO53bjejk": {
"date": "15\/12\/2019",
"discount": 0,
"due": -1.6,
"items": [
{
"pamount": 2.4,
"pname": "Granola",
"pprice": 2.4,
"punit": 1
}
],
"name": "James Brown",
"paym": [
{
"paymentAmnt": 4,
"paymentDesc": "Cash Payment :"
}
],
"subTotal": 2.4,
"time": "00:35:32",
"total": 2.4
},
"-LwANPC0UYCmTnqm7DRV": {
"date": "15\/12\/2019",
"discount": 1,
"due": 0,
"items": [
{
"pamount": 2.4,
"pname": "Granola",
"pprice": 2.4,
"punit": 1
},
{
"pamount": 2.6,
"pname": "Brownie",
"pprice": 2.6,
"punit": 1
},
{
"pamount": 2.4,
"pname": "Granola",
"pprice": 2.4,
"punit": 1
},
{
"pamount": 2.6,
"pname": "Brownie",
"pprice": 2.6,
"punit": 1
}
],
"name": "James Brown",
"paym": [
{
"paymentAmnt": 5,
"paymentDesc": "Cash Payment :"
},
{
"paymentAmnt": 4,
"paymentDesc": "Card Payment :"
}
],
"subTotal": 10,
"time": "21:05:43",
"total": 9
}
}
谁能告诉我,我哪里做错了?
谢谢。
编辑部分 …… 产品类别
data class Product(var pName: String = "", var pUnit: Int = 1, var pPrice: Double = 00.00, var pAmount: Double = 00.00) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readInt(),
parcel.readDouble(),
parcel.readDouble()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(pName)
parcel.writeInt(pUnit)
parcel.writeDouble(pPrice)
parcel.writeDouble(pAmount)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Product> {
override fun createFromParcel(parcel: Parcel): Product {
return Product(parcel)
}
override fun newArray(size: Int): Array<Product?> {
return arrayOfNulls(size)
}
}
另外,这里尝试过的部分。
val rootRef = FirebaseDatabase.getInstance().reference
val ref = rootRef.child("Sales").child("-LwIWKf-CTNTdwi4iMLU")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val product = dataSnapshot.getValue(Product::class.java)
//println("pname : "+product?.pName)
Log.d(TAG, product!!.pName)
}
override fun onCancelled(databaseError: DatabaseError) {
// Log.d("TAG", databaseError.getMessage()) //Don't ignore errors!
}
}
ref.addListenerForSingleValueEvent(valueEventListener)
另外,尝试了 Peter 的解决方案,但我需要将“项目”作为 ArrayList 谢谢
【问题讨论】:
-
您说的是哪个列表?如我所见,您只需要一种产品,对吧?
-
这里这个;
val generic: GenericTypeIndicator<ArrayList<Product>> = object : GenericTypeIndicator<ArrayList<Product>>() {} val items = h.child("items").getValue(generic) items?.let { productList.addAll(it) } -
我不明白。您尝试获取单个产品或产品列表?
-
产品列表。
标签: android firebase kotlin firebase-realtime-database android-recyclerview