如何根据Firebase中的特定键读取特定值?

时间:2017-04-13 08:40:53

标签: android firebase firebase-realtime-database

我的代码如下所示:

        mFirebaseDatabase = mFirebaseInstance.getReference();

        mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                allText.setText(dataSnapshot.child("58ca237b2e2c211dc0c7ed9").child("order_status").getValue(String.class));
                Log.d(TAG, "onDataChange: " + dataSnapshot.hasChild("58ca237b2e2c211dc0c7ed9"));
                Log.d(TAG, "onDataChange: next " + dataSnapshot.getValue().equals("58ca237b2e2c211dc0c7ed9"));
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    Log.d(TAG, "onDataChange for: " + child.toString());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG, "onCancelled: ");
            }
        });

我在日志中收到的回复如下所示:

onDataChange for: DataSnapshot { key = 58ca237b2e2c211dc0c7ed9, value = {order_status=2} }

在这里,我想阅读order_status=2,但我无法做到。我怎么能这样做?

我的firebase架构如下所示:

firebase schema

2 个答案:

答案 0 :(得分:0)

创建您期望的结果模型,然后简单地这样做,您可以操纵适合您的代码

public static class Post {

public String author;
public String title;

public Post(String author, String title) {
 this.author = author;
 this.title = title;
}

//Default Constructor
public Post {
}

//Setter and Getter below
 -----

//获取对我们帖子的引用

final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-
data/fireblog/posts");

//附上一个监听器以阅读我们的帖子参考

中的数据
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    Post post = dataSnapshot.getValue(Post.class);
    System.out.println(post.get(------));
}

@Override
public void onCancelled(DatabaseError databaseError) {
    System.out.println("The read failed: " + databaseError.getCode());
}
});

答案 1 :(得分:0)

您可以通过以下方式从FireBase发送/接收数据:

将数据发送到FireBase

EmpInfo empInfo = new EmpInfo();
                empInfo.setName(mEditTextName.getText().toString());
                empInfo.setAge(mEditTextAge.getText().toString());
                empInfo.setMobile(mEditTextMobileNo.getText().toString());
                empInfo.setCity(autoCompleteTextView.getText().toString());

                databaseReference.child("Emp_Info").setValue(empInfo);

从FireBase获取数据

databaseReference.child("Emp_Info").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d(TAG,"onDataChages invoked ="+dataSnapshot.toString());

                    //Getting the data from snapshot
                    EmpInfo empInfo = (EmpInfo)dataSnapshot.getValue(EmpInfo.class);
                    mTextName.setText(mTextName.getText()+" : "+empInfo.getName());
                    mTextAge.setText(mTextAge.getText()+" : "+empInfo.getAge());
                    mTextMobileNo.setText(mTextMobileNo.getText()+" : "+empInfo.getMobile());
                    mTextCity.setText(mTextCity.getText()+" : "+empInfo.getCity());


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

模型类

public class EmpInfo {
    public String name;
    public String age;
    public String mobile;
    public String city;

    public EmpInfo(){

    }

    public EmpInfo(String name, String age, String mobile,String city) {
        this.name = name;
        this.age = age;
        this.mobile = mobile;
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

希望它会对你有帮助!