如何使用firebase获取android中特定值的推送ID

时间:2018-05-11 11:41:46

标签: android firebase firebase-realtime-database

如何获取子项的值的推送ID(" topic_name")。getValue()=" algebre" ? FirebaseDatabase结构:

enter image description here

我已经尝试过push()。getKey();但它给我带来了另一把钥匙。

2 个答案:

答案 0 :(得分:1)

试试这个:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child(TOP_NODE_NAME).orderByChild("topic_name").equalTo("algebre");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    String key = child.getKey();
                }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

现在key拥有topic_name=algebra

节点的密钥

答案 1 :(得分:-1)

尝试这个来获取ID ..

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    reference.push().getKey();

并在子节点内搜索,然后进行如下查询..

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference.child("topic_name").equalTo("algebre").orderByKey();
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
           if (dataSnapshot.exists()) {

                for (DataSnapshot issue : dataSnapshot.getChildren()) {
                    Log.d("Value::", issue.getValue(Child.class).key); // field name you need to print.
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });