11

After retrieving group key in HashMap how to perform join query which shows only those grp details which have member as that particular user. And if this structure is wrong please help me with this.

Structure:

screenShot

muescha
  • 1,544
  • 2
  • 12
  • 22
Arjun Thakkar
  • 113
  • 1
  • 1
  • 7
  • See also https://groups.google.com/forum/#!searchin/firebase-talk/join|sort:date/firebase-talk/DKnBnI0bqoI/YMrp-L1hBgAJ – Kato Jan 30 '17 at 19:16

1 Answers1

3

Use DatabaseReference inside another DatabaseReference:

 // any way you managed to go the node that has the 'grp_key'
    DatabaseReference MembersRef = FirebaseDatabase.getInstance()
            .getReference()
            .child("Members")
            .child("1CkPG20Tt2dzrVkYkdfCLo")
            .orderByKey().equalTo("-KYnhiAucnasdkadNC")
            .addValueEventListener(
                    new ValueEventListener()
                    {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot)
                        {
                            for (DataSnapshot child : dataSnapshot.getChildren())
                            {

                                Map<String, Object> valuesMap = (HashMap<String, Object>) dataSnapshot.getValue();

                                // Get push id value.
                                String key = valuesMap.get("grp_key");


                                // HERE WHAT CORRESPONDS TO JOIN
                                DatabaseReference chatGroupRef = FirebaseDatabase.getInstance().getReference()
                                        .child("Chat_groups")
                                        .orderByKey().equalTo(key)
                                        .addValueEventListener(
                                                new ValueEventListener()
                                                {
                                                    @Override
                                                    public void onDataChange(DataSnapshot dataSnapshot)
                                                    {
                                                        // repeat!!
                                                    }

                                                    @Override
                                                    public void onCancelled(DatabaseError databaseError)
                                                    {

                                                    }
                                                }
                                        )
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError)
                        {

                        }
                    }
            );
amrro
  • 1,526
  • 17
  • 21
  • Can you please tell me how to combine this data with firebase recycler adapter and thanks alot #amrelghobary sir – Arjun Thakkar Dec 14 '16 at 21:37
  • @ArjunThakkar you can join it using [`FirebaseIndexRecyclerAdapter`](https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-with-indexed-data) – amrro Dec 15 '16 at 05:38
  • 1
    @ amrelghobary thank you sir. 1 last question if i have one group than all of this working fine, but what if i have multiple groups.That first MembersRef you have mentioned "orderByKey().equalTo("-KYnhiAucnasdkadNC")" here insted of "-KYnhiAucnasdkadNC" this single rendom key i have list of random keys than what should i do ? – Arjun Thakkar Dec 16 '16 at 19:14
  • I haven't understand your question very well, but `FirebaseIndexRecyclerAdapter` handles all the keys in the passed location for you. Remember to mark my response as answer, if it helped you! – amrro Dec 17 '16 at 04:40
  • @ amrelghobary sir can u explain how to take keyref and database reg in FirebaseIndexRecyclerAdapter , I have searched alot but i couldnt find a single tutorial on FirebaseIndexRecyclerAdapter. – Arjun Thakkar Dec 25 '16 at 00:21