How do I write and read fields and subfields in Firebase | Google?

0

Hi, I am trying to access the database provided by Google with Firebase, my summary code is simply this.

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = 
    database.getReferenceFromUrl("https://pruebasfirebase-23d0d.firebaseio.com/");

From now on I do not know how to work, I want to achieve something of this style:

-Raiz
+ SuperCampo
---- SubCampo
---- Subfield
+ SuperCampo
---- SubCampo
---- SubCampo

What I want to know is after getting my reference from the database with which methods to read a key-value pair (field) and also how to write, all with their respective hierarchy, although I think that knowing how to write and read from I would fix the root already.

    
asked by Parzival 17.07.2017 в 00:02
source

1 answer

0

Assuming that you have the correct read / write permissions (in the rules of the firebase database) and that you already have the app in firebase linked to your Android Studio app (the google-services.json file):

To read:

1) Create Reference, DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();

2) Do the "consultations" (to use a similar term):

Query mConsulta = mRootRef
            .child("SuperCampo").child("SubCampo");

3) Set a listener that will be aware of the changes when adding, changing, deleting, moving, etc. the data (I only include the aggregate but you can implement the other methods later)

ChildEventListener childEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            ClaseModelo mObjeto1 = dataSnapshot.getValue(ClaseModelo.class);

            String campo1 = mObjeto1.getCampo1();
            String campo2 = mObjeto1.getCampo2();

        }
}

Actually I would make a suggestion, for the reference it seems to me that you would be basing on old documentation of how it was done before. I suggest you follow these instructions that allow you to do what you want.

I hope you serve

    
answered by 17.07.2017 в 15:57