1

Here member’s key in Group is User’s key. Task: Want to fetch comida_id froenter code herem User based on Group’s member key. Here we are trying to first fetch member keys from Group. While fetching member key we also want to fetch User and User’s comida_id while that member’s loop is going. So the scenario will be First we are going to fetch all keys from Group based on Group’s key ( for example createTime, groupName, members ). Now from that we will execute loop on members so will get member’s key. Now while fetching this member’s key in that loop, we want to fetch comida_id from User’s, which will be fetch in that loop only.

GroupKey -> get Data(createTime, groupName, members, restaurants) -> fetch members key and comida_id ( in Loop (get member key then fetch comida_id ))

*My Code

FirebaseDatabase mDatabase_gp = FirebaseDatabase.getInstance();
mDatabase_gp.getReference("Group/"+group_key).addValueEventListener(
                new ValueEventListener()
                {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot)
                    {

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

                        Map userkey = (Map) valuesMap.get("members");
                        Log.d("Members", String.valueOf(userkey));

                        for (Object ke: userkey.keySet())
                        {
                            Log.d("runfirst","First Run");

                            String key = String.valueOf(ke.toString());
                            Log.d("Member key",key);


                            // HERE WHAT CORRESPONDS TO JOIN
                            FirebaseDatabase User = FirebaseDatabase.getInstance();
                            User.getReference("User/"+key).addValueEventListener(
                                    new ValueEventListener()
                                    {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot)
                                        {
                                            Map<String, Object> valuesMap = (HashMap<String, Object>) dataSnapshot.getValue();

                                            String userid = String.valueOf(valuesMap.get("comida_id"));
                                            Log.d("comida_id",userid);
                                            Toast.makeText(ChatSectionActivity.this,userid,Toast.LENGTH_SHORT).show();
                                        }
                                        @Override
                                        public void onCancelled(DatabaseError databaseError)
                                        {
                                        }
                                    }
                            );
                        }

                        Log.d("runlast","Last Run");
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError)
                    {

                    }
                }
        );

*

In Above code we tried to achieve same thing but here this code will fetch all member keys from group first. After fetching all members it will fetch comida_ids in another loop.. So there will be like two different loops working to fetch comida_id from members. It’s not working like join query.

GroupKey -> get Data(createTime, groupName, members, restaurants) -> fetch members key (in Loop (get member key) after then fetch comida_id .

*1) I was try this solution - How to perform join query in Firebase?

**But get this error –

Incompatible types.  
Required:   com.google.firebase.database.DatabaseReference 
Found:  com.google.firebase.database.ValueEventListener 

https://i.stack.imgur.com/ljlWo.png

Community
  • 1
  • 1

1 Answers1

0

Firebase doesn't have join queries. And what you are doing wrong is your are referencing your data base on Fireabaseuser, which is wrong.

mdatabaseReference.child("Answer").child("Answer"+QID).orderByChild("questionId").equalTo(QID).addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

            if (muftiAnswer_List.size() > 0)
                muftiAnswer_List.clear();

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


                final AnswerClass muftiAnswer = postsnapshot.getValue(AnswerClass.class);


           String ide=muftiAnswer.getMuftiId(); //getting AlimId From Class object


                Log.e(TAG, "Alim:"+ muftiAnswer.getMuftiId());


                //Using Query to get Alim Name From Data Table.!
                mdatabaseReference.child("users").child("Alim").child(ide).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override      //.orderByChild("alimId").equalTo(user_Id)
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        Log.e(TAG, "AlimNode:");


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

                            Log.e(TAG, "EqualNode:");


                            if(postSnapshot.getKey().equals("alimName"))
                            {
                                Log.e(TAG, "AlimNameNodeSnapShot:");

                                //Setting Alim Name in Current Object replcing id for name

                                muftiAnswer.setMuftiId(postSnapshot.getValue().toString());

                            }

                           // resultMuftiAnswer_List.add(muftiAnswer);

                        }//End of snapshot
                    }//ondata Change

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        // Failed to read value
                        Log.w(TAG, "Failed to read value.", databaseError.toException());
                    }
                });//End OF Alim Query
            }//end of comments

            circular_progress2.setVisibility(View.GONE);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(Details_Activity.this, "LOLOLOL", Toast.LENGTH_SHORT).show();
        }
    });

You have to use nested loops for doing joins like MYSQL

Query query = reference.child("issue").orderByChild("id").equalTo(0);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            // dataSnapshot is the "issue" node with all children with id 0
            for (DataSnapshot issue : dataSnapshot.getChildren()) {
                // do something with the individual "issues"
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Visit link for better understanding

https://firebase.googleblog.com/2013/10/queries-part-1-common-sql-queries.html

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
  • Thanks for reply , How can perform query under query - if you understand my problem. actually in first query get list of member key from Group, that time i was use for loop when each key get then create query to that instance i got that key info from user tabel. please help me that point – Dipesh Khatate Apr 10 '17 at 11:15
  • @DipeshKhatate please check the answer – Abdul Kawee Apr 10 '17 at 12:41
  • Thanks, actually the query was not get exact answer What i want to do is when first loop is called i am getting one key from that one key i want to make another query using that key and get value. But now i am getting all keys first and after that it makes query for another loop please see the database on this link - https://i.stack.imgur.com/ljlWo.png – Dipesh Khatate Apr 10 '17 at 14:04
  • Well you are getting an ide in above example and then using that ide to get further data from firebase , and its okay if your are getting all the data just use for each loop with postSnapshot and get the only key you want – Abdul Kawee Apr 11 '17 at 05:28
  • @DipeshKhatate for(DataSnapshot postSnapshot:dataSnapshot.getChildren()) { Log.e(TAG, "EqualNode:"); if(postSnapshot.getKey().equals("alimName")) { Log.e(TAG, "AlimNameNodeSnapShot:"); //Setting Alim Name in Current Object replcing id for name muftiAnswer.setMuftiId(postSnapshot.getValue().toString()); } – Abdul Kawee Apr 11 '17 at 05:29