user13378232
user13378232

Reputation:

Check Mutiple values into Firebase and Retrive respected Node

I have list of contacts in Map object i want to compare each contact into Firebase and retrieve that node if contact matched as value.

Ex: Suppose i have 123,345,567 as 3 contacts i want to get that complete node if contact inside node.

Firebase Structure

-Users
    -someId1
        -contact:123
        -fname:something
    -someId2
        -contact:345
        -fname:something
    -someId3
        -contact:567
        -fname:something
    -someId4
        -contact:980
        -fname:something

How do i retrieve those complete nodes if given contact matched into Firebase node.

I have written something like this

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
final DatabaseReference reference=rootRef.child("Users");
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {

        for (Map.Entry<String, String> singleContact : contacts.entrySet()) {
            query=reference.orderByChild("contact").equalTo(singleContact.getKey());
            if (dataSnapshot.hasChild(singleContact.getKey()))
                userModelObjects.add(dataSnapshot.child(singleContact.getKey()).getValue(FirebaseUserModel.class));

        }
}

Upvotes: 0

Views: 35

Answers (1)

Hasan Bou Taam
Hasan Bou Taam

Reputation: 4035

I am assuming the keys in your map are like the keys in your database:

//your reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");

//make a listener

ValueEventListener listener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

     //this loop will extract all the values in every random ID

     for(DataSnapshot ds : dataSnapshot.getChildren()){

     //extract the values

     String contact = ds.child("contact").getValue(String.class);
     String fname = ds.child("fname").getValue(String.class);

     //check if they exist in the map, and see what you can do.........


    }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
     //error getting data
    }
};
ref.addValueEventListener(listener);

Upvotes: 1

Related Questions