0

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"
      }
    }
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Dec 12 '22 at 11:45
  • 1
    Besides that, please edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data). – Alex Mamo Dec 12 '22 at 11:45
  • I don't have any errors but the logic to get the data of 7 days is not valid logic. – prabin chand Dec 12 '22 at 13:01
  • What does `FirebaseReference.databaseReference(getActivity())`? – Alex Mamo Dec 12 '22 at 14:04
  • This is my Custom class which is used to reference FirebaseDatabase instance. Please check out, i just edited my post. – prabin chand Dec 12 '22 at 14:35
  • Your `date` field is in a format that can't be queried through the database API. To allow retrieving a date range through a query, store the date in a format that can be queries, such as ISO 8601 (`2022-12-20`). See https://stackoverflow.com/q/38216858 and https://stackoverflow.com/a/41553469 – Frank van Puffelen Dec 12 '22 at 14:57

0 Answers0