如何检索数据? Firebase 3.0数据库

时间:2016-07-19 15:06:02

标签: android firebase firebase-realtime-database

如何从" compresor_1"中检索所有孩子?例如?

Firebase数据库:

"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",
    },

从网上尝试了这个例子,但它向我显示了这个错误:

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

更新1

已经尝试过这个:

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

而且:

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

出现同样的错误。

更新3

数据快照包含此信息,我如何划分每个对象?&#34; .get(&#34; modelo&#34;)使用该键检索第一个元素。

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}} }

工作代码

@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);
    }

1 个答案:

答案 0 :(得分:1)

如果每个节点都包含我从JSON文件中看到的相同子节点,则可以创建Compresores对象的模型类。

下面:

<强> 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;
    }
}

然后在你的听众中:

@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) {

        }
    });*/

这种面向对象的方法将为结构提供灵活性。