I have a firebase database that looks like this
I want to retrieve data from all months of 2022. I have a code that retrieves data from 2022 February that looks like this
val reference = database.getReference("users").child(uid).child("Incomes").child("2022").child("February")
reference.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val adapter = GroupAdapter<GroupieViewHolder>()
snapshot.children.forEach{
val expenseElement = it.getValue(ExpenseInHome::class.java)
if (expenseElement!=null){
adapter.add(ExpenseItemForHomeScreen(expenseElement))
}
incomeHomeRecycler.adapter = adapter
}
}
override fun onCancelled(error: DatabaseError) {
}
})
My intention is to get data from all months not just February. If I write
val reference = database.getReference("users").child(uid).child("Incomes").child("2022")
I get an error.
Is there a way to do this?
CodePudding user response:
I think it is not possible with a database structure like this. In my opinion the best thing you can do is think the database in a different way, organizing in another specific field what you want to get
CodePudding user response:
Assuming that your current code works for a single month, you can expand it to process all months in a year by adding an extra loop:
val reference = database.getReference("users").child(uid).child("Incomes").child("2022")
reference.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val adapter = GroupAdapter<GroupieViewHolder>()
snapshot.children.forEach{ //