1

How can i retrieve all the childs from "compresor_1" for example?

Firebase database:

"CompresoresEstandar" : {
    "Compresor_1" : {
    "Marca" : "SANDEN",
    "Modelo" : "505/5H09",
    "Canales" : "PV5",
    "Dimametro-MM" : "120",
    "Voltios" : "12",
    "Conexion" : "V-O",
    },
    "Compresor_2" : {
    "Marca" : "SANDEN",
    "Modelo" : "505/5H09",
    "Canales" : "PV5",
    "Dimametro-MM" : "120",
    "Voltios" : "12",
    "Conexion" : "V-O",
    },

Tried this example from the net but it shows me this error:

com.google.firebase.database.DatabaseException: Class java.util.Map has generic type parameters, please use GenericTypeIndicator instead

UPDATE 1

Tried already this:

GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {};
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator );

And this:

Map<String,String> map = dataSnapshot.getValue(Map.class);

With the same error.

UPDATE 3

Datasnapshot contains this information, how can i "divide each object?" .get("modelo") retrieve the first element with that key.

DataSnapshot { key = CompresoresEstandar, value = {Compresor_2={Letras Culata=, Voltios=12, Marca=SANDEN, Conexion=V-R, Canales=2A, RefACR=130002, Modelo=505/5H09, Dimametro-MM=125}, Compresor_3={Letras Culata=, Voltios=12, Marca=SANDEN, Conexion=V-C, Canales=2A, RefACR=130003, Modelo=505/5H09, Dimametro-MM=125}, Compresor_1={Letras Culata=, Voltios=12, Marca=SANDEN, Conexion=V-O, Canales=PV5, RefACR=130001, Modelo=505/5H09, Dimametro-MM=120}} }

WORKING CODE

@Override
public void onStart(){
    super.onStart();
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference compresoresRef = database.getReference("CompresoresEstandar");
    compresoresRef.orderByChild("Ref").addChildEventListener(new ChildEventListener() {

    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        dataSnapshot.getChildren();
        CompresorModel coe = dataSnapshot.getValue(CompresorModel.class);
        String modelo = coe.getModelo();
        String marca = coe.getMarca();
        String canales = coe.getCanales();
        textData1.setText(modelo);
        textData2.setText(marca);
        textData3.setText(canales);
    }
Community
  • 1
  • 1
Xaloju
  • 195
  • 1
  • 16

1 Answers1

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

public class CompresoresEstandarUnit {

    String Marca;
    String Modelo;
    String Canales;
    String Dimametro_MM; //Recommend you to change Firebase Node name to Dimametro_MM
    String Voltios;
    String Conextion;

    public CompresoresEstandarUnit() {
        //Firebase Empty Constructor
    }

    public String getCanales() {
        return Canales;
    }

    public void setCanales(String canales) {
        Canales = canales;
    }

    public String getConextion() {
        return Conextion;
    }

    public void setConextion(String conextion) {
        Conextion = conextion;
    }

    public String getDimametro_MM() {
        return Dimametro_MM;
    }

    public void setDimametro_MM(String dimametro_MM) {
        Dimametro_MM = dimametro_MM;
    }

    public String getMarca() {
        return Marca;
    }

    public void setMarca(String marca) {
        Marca = marca;
    }

    public String getModelo() {
        return Modelo;
    }

    public void setModelo(String modelo) {
        Modelo = modelo;
    }

    public String getVoltios() {
        return Voltios;
    }

    public void setVoltios(String voltios) {
        Voltios = voltios;
    }
}

And then in your listener:

@Override
public void onStart(){
    super.onStart();
    DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
    mRootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
         dataSnapshot.getChildren();
            CompresoresEstandarUnit coe = dataSnapshot.getValue(CompresoresEstandarUnit.class);


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

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });*/

This object oriented approach will provide flexibility to the structure.

Tanishq Sharma
  • 538
  • 2
  • 14
  • Got an error, but the Uppercase seems ok. I copied yours and revised it. FATAL EXCEPTION com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: canales – Xaloju Jul 21 '16 at 16:03
  • Please refer to this: [DatabaseException: Found two getters or fields with conflicting case sensitivity](http://stackoverflow.com/questions/37801036/databaseexception-found-two-getters-or-fields-with-conflicting-case-sensitivity) @Frank has explained profoundly here. – Tanishq Sharma Jul 21 '16 at 18:32
  • Also, have you changed ***Dimametro-MM*** node? See, as far as serializing practice goes, having the variables with exact same name of the node is the best way. Since Java doesn't support ```-```, I would recommend you to change your node key with something that can be serialized as a variable too. – Tanishq Sharma Jul 21 '16 at 18:34
  • Getting this error (similar to update 1) : Can't convert object of type java.lang.String to type CompresorModel – Xaloju Jul 22 '16 at 10:18
  • Can you please update the code once? I doubt that you are reading the child value instead of the node of Compresor... – Tanishq Sharma Jul 22 '16 at 15:54
  • My code now is the "Updated code" , i will remove the other. – Xaloju Jul 22 '16 at 15:58
  • @Xaloju You are using `ChildListener`, it will give you separate values of each child node. Instead, use `ValueEventListener`, the dataSnapshot then returns whole node that can be serialized in your custom java class. – Tanishq Sharma Jul 22 '16 at 16:00