0

I want to get the key of the child node in the Firebase Realtime Database, which is also the UID. I am randomly choosing a child node and now I want to get the key of that child so that I can do further operations on it.

databaseReferencePickers.addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
         try {
            int pickerCount = (int) dataSnapshot.getChildrenCount();
            Random random = new Random();
            int rand = random.nextInt(pickerCount);
            Iterator itr = dataSnapshot.getChildren().iterator();

            for(int i = 0; i <= rand; i++) {
                itr.next();
            }

            pickerDetails = itr.next().toString();

            ObjectMapper oMapper = new ObjectMapper();
            map = oMapper.convertValue(itr.next(), Map.class);
            //pickerUID = Objects.requireNonNull(map.get("key")).toString();
            pickerUID = dataSnapshot.getKey();
            Toast.makeText(OrderPickupActivity.this,pickerUID,Toast.LENGTH_LONG).show();

         } catch (Exception e) {
              Toast.makeText(OrderPickupActivity.this, e.toString(), Toast.LENGTH_LONG).show();
         }
     }

     @Override
     public void onCancelled(DatabaseError databaseError) {

     }
});

Can you please help me with it? Whenever I Toast the value of "PickerUID", I get the value as null.

Here is the image.

I want to retrieve the key from the pickers section.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Electric Dragon
  • 176
  • 5
  • 17

1 Answers1

0

If you want to get a random uid by attaching a listener on the Pickers node, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference pickersRef = rootRef.child("Pickers");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> uids = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String uid = ds.getKey();
            uids.add(uid);
        }

        String randomUid = new Random().nextInt(uids.size());
        //Do what you need to do with this random uid
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.d(TAG, "Error: ", task.getException()); //Don't ignore errors!
    }
};
pickersRef.addListenerForSingleValueEvent(valueEventListener);

See, you have to iterate through the DataSnapshot object using the getChildren() method in order to obtain the results. However, this is not quite the best solution because you download the entire Pickers node, in order to get a single random id. For a more appreciate solution, please also check my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Should I create a new ML kit for it or a cloud function? – Electric Dragon May 18 '20 at 12:09
  • "It's not working", doesn't help me, help you solve the issue. Show me your new database schema and the **EXACT** code that you are using. I personally test it and it works pretty fine. – Alex Mamo May 18 '20 at 12:27