0
votes

I am creating an android app and I am stuck at a problem. In order to explain the problem, I would like to show my database structure

{

 "EXlzg1COUbOhQjwPCGbS1NRdp5H3" : {
    "Contacts" : {
      "Contact1" : value (Contact number)
      "Contact2" : value
      "Contact3" : value
    },
    "name" : "Sagar Khan",
    "phone" : 7276273667
  },


 "OLm7VWsMcGQpterECyhJ8YTSPna2" :  {
    "Contacts" : {
      "Contact1" : value
      "Contact2" : value
      "Contact3" : value
    },
    "name" : "Sameer",
    "phone" : 8412914728
  },
  "TXanCkqtB5PdEogtv8Dzw8y1ngw1" : {
    ""Contacts" : {
      "Contact1" : value
      "Contact2" : value
      "Contact3" : value
    },
    "name" : "Harish",
    "phone" : 7020743544
  },
  "qnDVoc72nXa8XvOH1L39VvqFzKL2" : {
    "Contacts" : {
      "Contact1" : value
      "Contact2" : value
      "Contact3" : value
    },
    "name" : "Harish Shinde"
    "phone":  8149870822
  }
}

This is very short structure the actual one is to big Now what I want to do is I want to fetch only those users whose contact number is present in current user node and display those users in a Android list.

For example:

User 1 with id EXlzg1COUbOhQjwPCGbS1NRdp5H3 is having 3 contacts

"EXlzg1COUbOhQjwPCGbS1NRdp5H3" : {
    "Contacts" : {
      "Sameer" : 8412914728 (Contact number)
      "Contact2" : value
      "Contact3" : value
    },
    "name" : "Sagar Khan",
    "phone" : 7276273667
  },

Now when I will fetch the list of users from my database and show it in my Android app list, I want only those users to be added whose contact number is present in User 1 contacts.

My problem is that I literally don't know how to do this as I am new to Android development and Firebase. I am clear with the read and write basics of Firebase, but such operations, I have no idea how to do it.

I have searched a lot, but cannot find any solutions or examples. A detailed description will be very good for me. ;) Thanks in advance.

1
It would be easier to have a separate parent node just for Contacts of users, something like Contact -> (User ID) -> contact1 ..etc. It will be both easier for you to retrieve data and a user can store any number of contacts. I can post a code snippet how this approach will go if you're interested in changing the current database structure. - RamithDR
Yeah sure please ... But then how will i access the properties of user node? - Sagar Khan
As when i will fetch the list of users and display it in android list and when user will click on a specific item of the list the further operations will require accessing properties of that selected user which is under user node.... I hope u are understanding what i am trying to say - Sagar Khan
Your user node will be the same, let's say for the user with ID EXlzg1COUbOhQjwPCGbS1NRdp5H3 will have a child node under Users (containing name and phone number) and the contacts of that user will be stored in Contacts node again with an user ID EXlzg1COUbOhQjwPCGbS1NRdp5H3 . So basically there will be two parent nodes Users and Contacts. Do you get what I'm trying to say? - RamithDR
Is there any way i can get the id of a particular user by searching its contact number? - Sagar Khan

1 Answers

0
votes

Here's the change I propose:

This is the new database structure for Users node:

"Users" : {
  "EXlzg1COUbOhQjwPCGbS1NRdp5H3" : {
      "name" : "Sagar Khan",
      "phone" : 7276273667
    },
    ..
}

And the contacts of the user "Sagar Khan" will be stored in another node called Contacts under that user's UserID (EXlzg1COUbOhQjwPCGbS1NRdp5H3) :

"Contacts" : {
  "EXlzg1COUbOhQjwPCGbS1NRdp5H3" : {
      "Contact1":{
        "name" : "Sameer",
        "phone" : 7276273000
      },
      "Contact2":{
        "name" : "Alice",
        "phone" : 7276273600
      },
      ...
    },
    //other user id with  their contacts
    ...
  }

I suspect that the user ID (eg: EXlzg1COUbOhQjwPCGbS1NRdp5H3) is taken from a logged in user, using:

mAuth.getCurrentUser().getUid();

Therefore, to get the contacts of a logged in user you can query the database like this:

DatabaseReference  mUserContactsRef =  FirebaseDatabase.getInstance().getReference().child("Contacts").child(mAuth.getCurrentUser().getUid(););
mUserContactsRef.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {

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

              Contact contact = userContact.getValue(Contact.class);
              //add each contact to an arraylist and use it to populate a list of contacts

          }

      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
  });

You can add each contact object to an ArrayList and use it to populate a list. The key is the User ID, you can access anything related to a user using the UserID.

This is the approach I recommend and what I have used in the past successfully for similar scenarios.