Firebase在未知的推送键下获取值

时间:2017-02-06 10:00:41

标签: android firebase firebase-realtime-database

如果我对推送键一无所知,我怎样才能获得该值?

+users
  +9JZTuGUzc8bx7FLrwResWmp8L583 

     +anon:

     +email:

     +fid: <- i want to get this id without knowing push key (9JZTuGUzc8bx7FLrwResWmp8L583 )

     +username: 

目前,我正在尝试使用null响应:

FirebaseDatabase.getInstance().getReference().child("users").orderByChild("anon").equalTo(getIntent().getStringExtra(EXTRA_POST_USERNAME))
                    .addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            // Get user information
                            if(dataSnapshot.exists()){
                            User user = dataSnapshot.getValue(User.class);

                            fidanon = user.fid;}

                        }
                        @Override
                        public void onCancelled(DatabaseError databaseError) {}

                    });

1 个答案:

答案 0 :(得分:0)

final Query query = FirebaseDatabase.getInstance().getReference().child("users").orderByChild("anon").equalTo(getIntent().getStringExtra(EXTRA_POST_USERNAME));
            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot != null) {
                        final String fid = dataSnapshot.child("fid").getValue().toString();
                        Toast.makeText(getActivity(), fid, Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

使用此功能,将解决您的问题。