1

Firstly I would like to show you my Firebase database tree

-Users
   |
   ----959670000
   |      |
   |      ------name
   |
   + ----750890000
   +------859200000

I had a root node with child as a user. whenever new user registers it got new Unique Id... and the unique Id is the phone number. Now In My Android Application I retrieve all my contacts i.e. phone number present in my mobile number. All I want is to compare Each phone number in my contact to the phone number present in the firebase database so that I would get to know which user in my contact had got registered.

All I am trying to ask are the ways to achieve the bold part in my question please Help me....I am very upset since the past 4 days and trying to achieve. Anyways thanx in Advance....:-)

Oswald
  • 161
  • 1
  • 13
  • Have you tried anything yet? Because there are probably many ways to implement this feature and many specific places where you could be having problems. It would be good to see what approach you're taking and where you got stuck. – Frank van Puffelen Nov 25 '16 at 15:12
  • Thanx for replying, Yes @FrankvanPuffelen I have tried a way in which I was using a Firebase `equalTo()` Query to lookup the whole _Firebase database of Conacts_ and compare my phone contacts and firebase database contacts. But due to some reason or may be due to network legacy issues code doesn't get executed.. here I have also asked about that [link(]http://stackoverflow.com/questions/40789754/my-firebase-query-is-not-responding-to-childeventlistener?noredirect=1#comment68812700_40789754). So all I am asking is for some different approach. If possible please – Oswald Nov 25 '16 at 16:19
  • @FrankvanPuffelen, Now I am getting all the contacts from the Firebase and comapring them with the phone contacts. Is their any other way to solve this problem...Please help me regarding this comparison issue. – Oswald Dec 03 '16 at 14:08

1 Answers1

0
public boolean getAllUserList(final ArrayList<ContactPojo> contactList){//all contact list as an argument
        databaseReference();
        for (int i = 0; i < contactList.size(); i++) {
            checkingMatchedData(contactList.get(i).getPhone(),contactList.get(i));
        }
        return false;
    }

public void checkingMatchedData(String phone, final ContactPojo contPojo){
    Query query=reference.orderByChild("phone").equalTo(Long.parseLong(phone));
    ValueEventListener listener=new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue()!=null) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    UserPojo pojo = snapshot.getValue(UserPojo.class);
                    ContactPojo contactPojo = new ContactPojo();
                    contactPojo.setName(pojo.getName());
                    contactPojo.setPhone(String.valueOf(pojo.getPhone()));
                    contactPojo.setPhoto_uri(pojo.getImageUrl());
                    contactPojo.setUserId(pojo.getUserId());
                    contactPojo.setTypeOfData(101);//this is used to define type of user i.e 101 for registered user or 100 for unregistered
                    finalUserList.add(contactPojo);
                }
            }else {
                contPojo.setTypeOfData(100);
                finalUserList.add(contPojo);
            }
        }

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

        }
    };
    query.addListenerForSingleValueEvent(listener);
}
public void databaseReference(){
    reference= FirebaseDatabase.getInstance().getReference().child("Users");
}
  • 1
    Explain the answer in required details rather just giving a straight code snippet. Your explanation may help someone in the future as well. – Kiran Maniya Sep 21 '19 at 18:50
  • Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From [review](https://stackoverflow.com/review). – Trenton McKinney Sep 21 '19 at 19:03
  • If my phonebook has 1000 users, then this way comparing all phonebook contacts would require 1000 firebase valueeventlisteners. Is it feasible to compute that? – Sourav Kannantha B Nov 29 '20 at 14:59