Android Firebase从post元素获取帖子ID

时间:2018-06-05 22:19:57

标签: android firebase firebase-realtime-database

我的火力库数据库:

enter image description here

我想从post元素中获取post id。

我知道authorId,createdDate,createdDateText和title,但我想获得帖子ID。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

请尝试使用像bellow一样的firebase查询,

Query query = FirebaseDatabase.getInstance().getReference().child("posts");
        query.orderByChild("title").equalTo("test");//Here replace title with your key which you want and replace test with value throw which search
        //query.orderByChild("title").equalTo("test").limitToFirst(1); //If you want to only one value
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //you get here the list of post from  DB 
                if (dataSnapshot.exists()) {
                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        String postId = child.getKey(); //Post Id
                        Logger.print("postId");
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

答案 1 :(得分:0)

尝试最终的字符串postId = getRef(position).getKey();