2

I am trying to display the phonebook contacts' numbers in a RecyclerView but only those are registered in the Firebase database where the contacts numbers are the key.

For that, I am trying to compare an ArrayList from the phone book and one from Firebase database and then saving the results in a third ArrayList that I aim to use it in the RecyclerViewAdapter.

The two problems:

  • I can only access Firebase database data inside the listeners of the references, so the size of the third ArrayList outside these listeners is always 0.
  • I can not use FirebaseRecyclerAdapter to display the custom third ArrayList.

So, how can I compare these values to get a third accessible ArrayList?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

0

Assuming that you have declared 2 lists that look like this:

List<String> phoneBookList = new ArrayList<>();
List<String> finalList = new ArrayList<>();

And you have a database structure that looks like this:

Firebase-root
  |
  --- firebaseDatabasePhoneNumbers
            |
            --- firebaseDatabasePhoneNumberKeyOne
            |       |
            |       --- //details
            |
            --- firebaseDatabasePhoneNumberKeyTwo
                    |
                    --- //details

To achieve what you want, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference firebaseDatabasePhoneNumbersRef = rootRef.child("firebaseDatabasePhoneNumbers");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> firebaseDatabaseList = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String firebaseDatabasePhoneNumber = ds.getKey();
            firebaseDatabaseList.add(firebaseDatabasePhoneNumber);
        }

        for(String phoneBookNumber : phoneBookList) {
            if (firebaseDatabaseList.contains(phoneBookNumber)) {
                finalList.add(phoneBookNumber);
            }
        }

        //Do what you need to do with the finalList
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
firebaseDatabasePhoneNumbersRef.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Is there everything alright? Have you tried my solution? – Alex Mamo Mar 23 '18 at 09:45
  • Hi Alex, thanks for your reply and sorry for the late reply, the problem with this solution is that I am not able to access the data out of the listener even if I change(init) them inside the listener, I get the first value out of it. –  Mar 24 '18 at 17:59
  • That's correct, you cannot simply use your `firebaseDatabaseList` outside that listener and this is because `onDataChange()` has an asynchronous behaviour. To solve this, use the `firebaseDatabaseList` only inside the `onDataChange()` method or, please see the last part of my answer from this [post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774). – Alex Mamo Mar 25 '18 at 07:31
  • 1
    Amazing, I am going to try this solution! –  Mar 25 '18 at 13:29
  • is it possible to use your solution in the format suggested by Google Android Architecture (`Model-View-ViewModel`)? If the answer is yes, could you place a solution in this stackoverflow question: https://stackoverflow.com/q/57317023/4300670 – Aliton Oliveira Aug 08 '19 at 23:40
  • @AlitonOliveira Sure it is possible, actually it is the recommended way of doing that. But I'm afraid this is really a bit too broad to reasonably be able to answer on Stack Overflow, that's why I see didn't get an answer so far. – Alex Mamo Aug 09 '19 at 11:35
  • @Alex, what if we have like thousands of children under that node, is your answer valid. Take for example app like WhatsApp, according to your solution it has to load 1billion userdata, and then loop through to get matching contacts for a user? – Jason Sep 05 '20 at 16:31
  • @Jason If you have billion of numbers then you should categorize them so you can download as less as possible. – Alex Mamo Sep 06 '20 at 09:32
  • Will Firebase query be a good idea in this case, if yes. Can you show how?? – Jason Sep 06 '20 at 12:25
  • @Jason Why not? I'm afraid the answer to that question, cannot be simply added as a comment. So I recommend you post a new question, so I and other Firebase developers can take a look at it. – Alex Mamo Sep 06 '20 at 12:31