Skip to content Skip to sidebar Skip to footer

How To Retrieve The Data ? Firebase 3.0 Database

How can i retrieve all the childs from 'compresor_1' for example? Firebase database: 'CompresoresEstandar' : { 'Compresor_1' : { 'Marca' : 'SANDEN', 'Modelo' : '505/5H0

Solution 1:

You can create a model class of your Compresores Object if every node contains same children as I can see from your JSON file.

Here:

CompresoresEstandarUnit.class

publicclassCompresoresEstandarUnit {

    StringMarca;
    StringModelo;
    StringCanales;
    StringDimametro_MM; //Recommend you to change Firebase Node name to Dimametro_MMStringVoltios;
    StringConextion;

    publicCompresoresEstandarUnit() {
        //Firebase Empty Constructor
    }

    publicStringgetCanales() {
        returnCanales;
    }

    publicvoidsetCanales(String canales) {
        Canales = canales;
    }

    publicStringgetConextion() {
        returnConextion;
    }

    publicvoidsetConextion(String conextion) {
        Conextion = conextion;
    }

    publicStringgetDimametro_MM() {
        returnDimametro_MM;
    }

    publicvoidsetDimametro_MM(String dimametro_MM) {
        Dimametro_MM = dimametro_MM;
    }

    publicStringgetMarca() {
        returnMarca;
    }

    publicvoidsetMarca(String marca) {
        Marca = marca;
    }

    publicStringgetModelo() {
        returnModelo;
    }

    publicvoidsetModelo(String modelo) {
        Modelo = modelo;
    }

    publicStringgetVoltios() {
        returnVoltios;
    }

    publicvoidsetVoltios(String voltios) {
        Voltios = voltios;
    }
}

And then in your listener:

@OverridepublicvoidonStart(){
    super.onStart();
    DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
    mRootRef.addListenerForSingleValueEvent(newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
         dataSnapshot.getChildren();
            CompresoresEstandarUnit coe = dataSnapshot.getValue(CompresoresEstandarUnit.class);


            String modelo = coe.getModelo();
            mConditionTextView.setText(modelo);
        }

        @OverridepublicvoidonCancelled(DatabaseError databaseError) {

        }
    });*/

This object oriented approach will provide flexibility to the structure.

Post a Comment for "How To Retrieve The Data ? Firebase 3.0 Database"