如何访问firebase中的特定根目录

时间:2018-04-22 11:21:26

标签: android firebase-realtime-database

我对firebase有疑问。

我有一个firebase数据库,想问一下如何访问日期根目录?初始化databaseReference时应该写什么。在这种情况下,4月12日上午08:48

enter image description here

2 个答案:

答案 0 :(得分:1)

DatabaseReference userlistReference = FirebaseDatabase.getInstance().getReference().child("Users");
Query donors = userlistReference.orderByChild("Email");
    donors.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getChildren() != null)
            {

                for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                String uid = snapshot.getKey();     
                if (snapshot.getValue() != null)
                    {
                        if (snapshot.child("Name").getValue() != null)
                        {
                            name = snapshot.child("Name").getValue().toString();
                        }
                        else
                        {
                            name = "";
                        }
                        if (snapshot.child("Email").getValue()  != null)
                        {
                            email = snapshot.child("Email").getValue().toString();
                        }
                        else
                        {
                            email = "";
                        }
                    }
}

你可以根据你的问题做这样的事情。你必须移动你的孩子,你的孩子需要root,然后你必须调用getKey()函数来获取root。我已经分享了我自己项目的代码,它对我有用。这与你的情况相同。在我的情况下,我必须得到uid,我通过snapshot.getKey()得到它。您可以通过此链接查看我的图像。 This is in my case

现在根据我的情况你应该试试

DatabaseReference userlistReference = FirebaseDatabase.getInstance().getReference().child("Video_Upload").child("Street");
Query donors = userlistReference.orderByChild("DateTime");
    donors.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getChildren() != null)
            {

                for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                String date = snapshot.getKey();     
                }
            }
        }
}

答案 1 :(得分:0)

您需要在此处查看tutorial

private DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference().child("Video_Upload").child("Street");

List<Street> streetList = new ArrayList<Street>();    

ValueEventListener streetListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot streetSnapshot: dataSnapshot.getChildren()) {
            String date = streetSnapshot.getKey(); // This will give you 12 Apr 08:48 AM for the first entry
            Street street = streetSnapshot.getValue(Street.class);
            streetList.add(street);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Street failed, log a message
    }
};

mDatabase.addValueEventListener(streetListener);

Street.java类看起来应该是这样的。

public class Street {
    public String DateTime; 
    public String Likes;
    public String Long_Desc;
    public String Short_Desc;
    public String UserName;
    public String VideoName;
    public String VideoPath;  

    // ... Other variables    
}