I'am new to programming. I want to get all the data of an week from firebase database when radio button is checked . How can i achieve that ?
FirebaseReference Class
package com.prabinchand.myexpensev2;
import android.content.Context;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.Objects;
public class FirebaseReference {
public static GoogleSignInAccount account;
public static DatabaseReference databaseReference;
public static DatabaseReference incomeReference;
public static DatabaseReference databaseReference(Context context) {
account = GoogleSignIn.getLastSignedInAccount(context.getApplicationContext());
databaseReference = FirebaseDatabase.getInstance().getReference()
.child("UsersExpense").child(Objects.requireNonNull(account.getDisplayName()));
return databaseReference;
}
public static DatabaseReference incomeDBReference(Context context) {
account = GoogleSignIn.getLastSignedInAccount(context.getApplicationContext());
incomeReference = FirebaseDatabase.getInstance().getReference()
.child("UsersIncome").child(Objects.requireNonNull(account.getDisplayName()));
return incomeReference;
}
}
weeklyCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()){
if (getActivity()!=null){
showTodayDate();
FirebaseReference.databaseReference(getActivity())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
String getDate = dataSnapshot.child("time").getValue(String.class);
}
Query q = FirebaseReference.databaseReference(getActivity()).orderByChild("time")
.equalTo(Calendar.DAY_OF_YEAR, String.valueOf(-7));
adapter.updateOptions(new FirebaseRecyclerOptions.Builder<ExpenseModel>()
.setQuery(q, ExpenseModel.class).build());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}else {
Toast.makeText(getActivity(), "Weekly unchecked !", Toast.LENGTH_SHORT).show();
}
}
});
Below is the JSON structure of my database reference. From Users Expense
i want to get all the data of a week, or can say data from 7 days including today.
How can i code that gets all the data for 7 days or a week?
JSON structure
{
"Users": {
"Prabin Chand": {
"email": "prabinchand77@gmail.com",
"name": "Prabin Chand",
"picture": "https://lh3.googleusercontent.com/a/AEdFTp7oaRy1fb0vs9ZYwGamVup5a7zatX5M5EfOeDNmrg"
},
"gibl rpa": {
"email": "gibl.rpa@gmail.com",
"name": "gibl rpa",
"picture": "null"
},
"prabin chand": {
"email": "prabinchand333@gmail.com",
"name": "prabin chand",
"picture": "https://lh3.googleusercontent.com/a/ALm5wu30fbdm8MUC0moXMg1Xjr5N002LOR1wXnfdEOX4"
}
},
"UsersExpense": {
"Prabin Chand": {
"-NImTBCIMgzWSDc3Kv-9": {
"amount": "250",
"description": "Gros",
"exType": "Grocery",
"milli": -1670515310217,
"time": "Thu, 08/12/2022"
},
"-NImTEqAMkKijjtGr46u": {
"amount": "450",
"description": "Vada deko",
"exType": "Rent",
"milli": -1670515325121,
"time": "Thu, 08/12/2022"
},
"-NImTHA4Qk-MY-Bc56ij": {
"amount": "1500",
"description": "Fuel",
"exType": "Bike",
"milli": -1670515334652,
"time": "Thu, 08/12/2022"
},
"-NImX8bXGd-dTZbctQEB": {
"amount": "500",
"description": "Beer",
"exType": "Beer",
"milli": -1670516755479,
"time": "Tue, 6/12/2022"
},
"-NIpyTVQwIB4L7e143Xt": {
"amount": "45",
"description": "Ajaja",
"exType": "Grocery",
"milli": -1670574106276,
"time": "Fri, 09/12/2022"
},
"-NIpyZ73MtQPh9nmoAnR": {
"amount": "45",
"description": "Ajai",
"exType": "Bike",
"milli": -1670574129294,
"time": "Fri, 09/12/2022"
},
"-NIpycMQKnGrfqaEKKV7": {
"amount": "10",
"description": "Beer aja",
"exType": "Beer",
"milli": -1670574146661,
"time": "Fri, 09/12/2022"
},
"-NIq50Tcd8VZJHvQihSy": {
"amount": "45",
"description": "Hsksk",
"exType": "Rent",
"milli": -1670576084282,
"time": "Fri, 09/12/2022"
},
"-NJ4ARWmLeaIsdnxmkXi": {
"amount": "45",
"description": "Aja",
"exType": "Grocery",
"milli": -1670829164672,
"time": "Mon, 12/12/2022"
}
}
}
}