【问题标题】:how to retreive a list of data from firebase and store it into ArrayList<>如何从 firebase 检索数据列表并将其存储到 ArrayList<>
【发布时间】:2020-04-08 12:40:22
【问题描述】:

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

产品型号很好,工作正常。

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

但不知何故,我无法从数据库中获取“项目”列表。 我想要的是获取pnamepunitppricepamount并将其存储在ArrayList&lt;Product&gt;

我对数据库的引用是这样的;

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

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

谢谢。

【问题讨论】:

  • 可以导出文件数据firebase,我要导入firebase和测试
  • 您在问题中包含了 JSON 树的图片。请将其替换为实际的 JSON 作为文本,您可以通过单击 your Firebase Database console 的溢出菜单 (⠇) 中的 Export JSON 链接轻松获得。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,一般来说这只是一件好事。
  • 您好,感谢您的关注。我在问题中添加了 json 文件。
  • 我回答你可以查
  • 我注意到这种方式只能从 firebase 获取所有产品条目。是否可以使其仅根据特定的键值显示。 (例如,我希望它只显示“-Lw5Qx8sVdw3ZCMiXAL-”键下的产品列表?

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


【解决方案1】:

你能不能试试:使用 GenericTypeIndicator 获取 List 数据。

可以参考我的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
}

【讨论】:

  • 您好,感谢您的回答,但它给出了错误com.google.firebase.database.DatabaseException: Generic wildcard types are not supported
  • 您可以将 List 修复为 ArrayList: val generic: GenericTypeIndicator> = object : GenericTypeIndicator>() {}
  • 感谢 @Cường Nguyễn 与 ArrayList 合作。很好的解决方案。
猜你喜欢
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-05-26
  • 2021-05-02
  • 1970-01-01
相关资源
最近更新 更多