David Green
David Green

Reputation: 83

how to retreive a list of data from firebase and store it into ArrayList<>

I have a Firebase database and I want to retreive a list of data from there and store into ArrayList.

Product model is fine, working.

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

But somehow I cannot get the list of "items" from database. What I want is to get pname, punit, pprice, and pamount and store it in ArrayList<Product>

enter image description here

my reference to database is this;

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

here is the json file

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

But, please help me what I am doing wrong here. any help appreciated.

Thanks.

Upvotes: 0

Views: 138

Answers (1)

Cuong Nguyen
Cuong Nguyen

Reputation: 1018

Can you try: Use GenericTypeIndicator to get List data.

You can refer my 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
}

Upvotes: 1

Related Questions