Mohammad Yusuf
Mohammad Yusuf

Reputation: 77

How to get the child from firebase in android studio?

enter image description here

In Firebase Realtime Database I want to show the user the list of orders that has been placed and for that, I need to get all the child that is created in Order Along with the it's value.

This is the code I have done so far

  FirebaseDatabase.getInstance().getReference().child("Users").child("Customer").child(userID).child("Order").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                Users usersnapshot = snapshot.getValue(Users.class);
                if(usersnapshot!=null){
                    mCustomerinfo.setVisibility(View.VISIBLE);
                    Map<String, Object> map = (Map<String, Object>) snapshot.getValue();




                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

    }
    public void Item( Map<String,Object> map){
        // TODO:To check all the item in order and show it to the user 
    }

Upvotes: 2

Views: 1783

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

If you only need the value of the "Egg" property, create a reference that points to the exact property and get it's value as in following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference eggRef = rootRef.child("Users").child("Customer").child(uid).child("Order").child("Egg");
    productsRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            String value = task.getResult().getValue(String.class);
            Log.d("TAG", value);
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result in the logcat will be:

10

If you want a user to have multiple orders, you should consider using a schema that looks like this:

Firebase-root
  |
  --- Users
       |
       --- $uid
            |
            --- orders
                 |
                 --- $pushedId
                 |     |
                 |     --- //Order details
                 |
                 --- $pushedId
                       |
                       --- //Order details

So you need to differentiate each order by calling push() method.

Edit:

According to your last comment:

I wanted to know the code for getting multiple children inside the order (Suppose Egg:10 Onion:5 and so on ) so how do I get that?

Here is your code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference orderRef = rootRef.child("Users").child("Customer").child(uid).child("Order");
orderRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String key = ds.getKey();
                String value = ds.getValue(String.class);
                Log.d("TAG", key + ":" + value);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result in the logcat will be:

Egg:10
Onion:5

The result in the

Upvotes: 1

Related Questions