0

I have a database of users with their emails, role and list of contacts.

enter image description here

I have code to add contacts that when you input into the EditText, it checks through the database if the email exists and if it does, it appends the email into the contacts list. However, nothing gets added in my database.

    public void searchContacts(final String emailInput){
    final DatabaseReference users;

    users = FirebaseDatabase.getInstance().getReference("users");
    users.orderByChild("email").equalTo(emailInput).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot userSnapshot: dataSnapshot.getChildren()){
                if((userSnapshot.child("email").getValue(String.class).equals(emailInput))){
                    users.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("contacts").child(emailInput).setValue(true);
                }
            }
        }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
zkvsl
  • 87
  • 1
  • 7

1 Answers1

1

Nothing is added, because you haven't create a list yet. To solve this, please use the following code:

public void searchContacts(final String emailInput){
    DatabaseReference users = FirebaseDatabase.getInstance().getReference("users");
    users.orderByChild("email").equalTo(emailInput).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<String> list = new ArrayList<>(); //Create the list
            for (DataSnapshot userSnapshot: dataSnapshot.getChildren()){
                String email = userSnapshot.child("email").getValue(String.class);
                if(email.equals(emailInput)){
                    users.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("contacts").child(emailInput).setValue(true);
                    list.add(email); //Add the email the list
                }
            }

            //Do what you need to do with list
        }
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I have that in my signup. I initialize a list with a string so that the list is not empty on initialization. If I follow your code, everytime I run the function wouldn't it create a new list with only one value? – zkvsl Oct 01 '18 at 13:12
  • Yes, because you are passing as an argument a single `emailInput`. So everytime you call this method, is called with a single argument. You cannot declare the `List` outside the callback because it will always be empty, due the asynchronous behavior of `onDataChange()` method. More informations **[here](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)**. So to solve this, you need to get a list of **all** contacts and then check if the email exists? And if it exists, add it to another list, right? – Alex Mamo Oct 01 '18 at 13:27
  • I kinda understand now. However I tried that and it still doesn't add the email of another user that exists that i entered into the EditText box into the contacts list on my database. – zkvsl Oct 01 '18 at 13:40
  • In this case you should make again your own attempt given the information in the answer/comments, and ask another question if something else comes up. – Alex Mamo Oct 01 '18 at 13:46
  • Why does my function not go into the for loop? – zkvsl Oct 02 '18 at 08:18
  • As I already advised you, please post another fresh question sa we can help you. I cannot be much of a help without seeing the code. – Alex Mamo Oct 02 '18 at 09:47