Home > Software engineering >  How to retrive as custom object using Firebase RTDB
How to retrive as custom object using Firebase RTDB

Time:03-01

public class FireStoreData  {

private String brgy;
private String street;
private String date;
private String  time;
private GeoPoint location;
private String item;


public FireStoreData(){

}

public FireStoreData(String brgy, String street, String date, String time, GeoPoint location, String item) {
    .....
}

...

Here is my database my database structure

This is my code for retrieving data

 databaseReference = FirebaseDatabase.getInstance().getReference();
    databaseReference.child( "CrimeReport" ).get().addOnCompleteListener( new OnCompleteListener<DataSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DataSnapshot> task) {
            if (!task.isSuccessful()) {

                Log.e("firebase", "Error getting data", task.getException());
            }
            else {


                Toast.makeText( requireContext(), "Data"  task.getResult().getValue(), Toast.LENGTH_SHORT ).show();
                Log.d("firebase", String.valueOf(task.getResult().getValue()));
            }
        }
    } );

This is the received data.

 {Palatie={Robbery={Report={date=02/28/22, item=Robbery, street=Santos, location={latitude=14.561677776545071, longitude=121.08633846044542}, time=12:0 AM, brgy=Palatie}}, Hijacking={Report={date=02/23/22, item=Hijacking, street=Santos, location={latitude=14.561677776545071, longitude=121.08633846044542}, time=12:0 AM, brgy=Palatie}}}

CodePudding user response:

Your actual database doesn't allow you to simply query the data. Since you're already storing the date into the date filed, you can remove one level in your three like this:

Firebase-root
  |
  --- CrimeReport
        |
        --- Baltazar
              |
              --- Robbery
                    |
                    --- $pushedId
                           |
                           --- brgy: "Baltazar"
                           |
                           --- date: "Feb 14, 2022"
                           |
                           --- \\The other fields.

Now to be able to read the data, you need to create a reference that points exactly to the Robbery node and then attach a listener as below:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference robberyRef = db.child("CrimeReport").child("Baltazar").child("Robbery");
robberyRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            for (DataSnapshot ds : snapshot.getChildren()) {
                String brgy = ds.child("brgy").getValue(String.class);
                Log.d("TAG", brgy);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

The result in the logcat will be:

Baltazar

In the same way, I read the value of the brgy field, you can also read the remaining ones.

If you want to map the result into an object of type FireStoreData, then instead of reading each value, you can map it directly using:

FireStoreData data = ds.getValue(FireStoreData.class);
Log.d("TAG", data.getBrgy());

This will produce the same result as above.

  • Related