Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从firebase检索数据列表并将其存储到ArrayList_Android_Firebase_Kotlin_Arraylist_Firebase Realtime Database - Fatal编程技术网

Android 如何从firebase检索数据列表并将其存储到ArrayList

Android 如何从firebase检索数据列表并将其存储到ArrayList,android,firebase,kotlin,arraylist,firebase-realtime-database,Android,Firebase,Kotlin,Arraylist,Firebase Realtime Database,我有一个Firebase数据库,我想从那里检索数据列表并存储到ArrayList中 产品型号很好,工作正常 class Product(var pName: String?, var pUnit: Int, var pPrice: Double, var pAmount: Double) 但不知何故,我无法从数据库中获取项目列表。 我想要的是获取pname、punit、pprice和pamount并将其存储在ArrayList中 我对数据库的引用是这样的 ref = FirebaseData

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

产品型号很好,工作正常

class Product(var pName: String?, var pUnit: Int, var pPrice: Double, var pAmount: Double)
但不知何故,我无法从数据库中获取项目列表。 我想要的是获取pname、punit、pprice和pamount并将其存储在ArrayList中

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

 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文件

但是,请帮帮我,我做错了什么。谢谢你的帮助


谢谢。

您能否尝试:使用GenericTypeIndicator获取列表数据

您可以参考我的github:


你能导出文件数据firebase吗?我想导入firebase并测试你的问题中包含了JSON树的图片。请将其替换为实际的JSON文本,您可以通过单击溢出菜单中的导出JSON链接轻松获得该文本⠇ 属于将JSON作为文本使其具有可搜索性,使我们能够轻松地使用它来测试您的实际数据,并在我们的答案中使用它,这通常是一件好事。您好,感谢您的关注。我在问题中添加了json文件。我回答了您可以检查的问题。我注意到这种方式只能从firebase获取所有产品条目。是否可以使其仅基于特定的键值显示。例如,我希望它只显示`-Lw5Qx8sVdw3ZCMiXAL-`key?Hi'下的产品列表,感谢您的回答,但它给出了一个错误com.google.firebase.database.DatabaseException:不支持通用通配符类型您可以将列表修复为ArrayList:val-Generic:GenericTypeIndicator=object:GenericTypeIndicator{}谢谢,@Cườ吴恩圭ễ与ArrayList合作的n。很好的解决方案。
{
"-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
    }
    }
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
}