如何从Firebase中检索数据列表并将其存储到ArrayList <>

时间:2019-12-16 02:58:00

标签: android firebase kotlin arraylist firebase-realtime-database

我有一个Firebase数据库,我想从那里检索数据列表并存储到ArrayList中。

产品模型很好,可以正常工作。

class Product(var pName: String?, var pUnit: Int, var pPrice: Double, var pAmount: Double)

但是以某种方式我无法从数据库中获取“项目”列表。 我想要的是获取pnamepunitppricepamount并将其存储在ArrayList<Product>

enter image description here

我对数据库的引用是这个

 ref = FirebaseDatabase.getInstance().getReference().child("Sales")

    ref.addValueEventListener(object : ValueEventListener{
        override fun onCancelled(p0: DatabaseError) {

        }

        override fun onDataChange(p0: DataSnapshot) {

            var sira: Int = 0

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


                            val dateR = h.child("date").value.toString()
                            val discountR = h.child("discount").value.toString()
                            val dueR = h.child("due").value.toString()
                            val nameR = h.child("name").value.toString()
                            val totalR = h.child("total").value.toString()
                            val subtotalR = h.child("subTotal").value.toString()
                            val timeR = h.child("time").value.toString()

                            val name = h.child("pname").value.toString()
                            val unit = h.child("punit").value.toString()
                            val price = h.child("pprice").value.toString()
                            val amount = h.child("pamount").value.toString()


                            productList.add(Product(name, unit.toInt(), price.toDouble(), amount.toDouble()))

这是json文件

{
"-Lw37--uyfuBA0AfTalp" : {
"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
    }
    }

但是,请帮助我在这里做错了什么。任何帮助表示赞赏。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试:使用GenericTypeIndicator获取列表数据。

您可以引用我的github:https://github.com/vancuong0429/stack_59350042

private lateinit var database: DatabaseReference
private var productList: ArrayList<Product> = arrayListOf()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    database = FirebaseDatabase.getInstance().getReference("Sales")
    database.addValueEventListener(object: ValueEventListener {
        override fun onCancelled(p0: DatabaseError) {

        }

        override fun onDataChange(p0: DataSnapshot) {
            productList.clear()
            for (snapshot in p0.children) {
                if (snapshot.hasChild("items")) {
                    val generic: GenericTypeIndicator<List<Product>> =
                        object : GenericTypeIndicator<List<Product>>() {}
                    val items = snapshot.child("items").getValue(generic)
                    items?.let { productList.addAll(it) }
                }
            }
        }

    })
}
class Product {
    var pName: String? = null
    var pUnit: Int? = null
    var pPrice: Double? = null
    var pAmount: Double? = null
}