androiddatabaseloopsfirebase-realtime-databasesmsmanager

Getting all firebase data through loop


I have a firebase database where contacts are saved. I want to fetch all the numbers of contacts and send them message one by one. I read it could be done by a for loop but the loop only returns the last number inserted, not all the numbers. Here is the code below:

 dbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            ArrayList<String> Userlist=new ArrayList<String>();

            for (DataSnapshot dsp : snapshot.getChildren())
               Userlist.add(String.valueof(dsp.getValue()));

           for(int i = 0; i<Userlist.size();i++)
{
show.setText(Userlist.get(i);
}
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Please tell me a method through which I can fetch all the numbers of contacts and send them message one by one. The code for sending the message is given below:

String phoneNumber="1234567890";

        String message="I need help";
        SmsManager smsManager=SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber,null,message,null,null);

My database structure is attached as well. Please have a look and guide me. I will be thankfuk to you.enter image description here


Solution

  • I have a firebase database where contacts are saved. I want to fetch all the numbers of contacts and send them message one by one. I read it could be done by a for loop but the loop only returns the last number inserted, not all the numbers.

    The problem is you're trying to set ArrayList items to TextView with a loop.

    for(int i = 0; i<Userlist.size();i++){
        show.setText(Userlist.get(i);
    }
    

    Setting text to TextView with loop changes TextView's text with every step of the loop so the TextView will show only the last item of the list. If you only use ArrayList for putting String into it and setting for TextView (according to your code it's), using ArrayList is useless. Inside onDataChange() method before getting your data, create an empty String and while you're getting your data expand your String with data. At the end of for loop String text will have all data and you can set it to TextView.

    dbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
             String text = "";
             for (DataSnapshot dsp : snapshot.getChildren()){
                 Map<String, Object> datas = (Map<String, Object>) dsp.getValue();
                 text += datas.get("number").toString() + "\n";
             }
             tv.setText(text);
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    

    Please tell me a method through which I can fetch all the numbers of contacts and send them message one by one.

    You have 2 options for getting your values. You can use Map or you can create your Java Object.

    Using Map:

    private void sendMessage(DatabaseReference dbRef) {
            dbRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    SmsManager smsManager= SmsManager.getDefault();
                    for (DataSnapshot dsp: snapshot.getChildren()){
                        Map<String, Object> datas = (Map<String, Object>) dsp.getValue();
                        String phoneNumber = datas.get("number").toString();
                        String message="I need help";
                        smsManager.sendTextMessage(phoneNumber,null,message,null,null);
                    }
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                }
            });
    }
    

    Using Java Object:

    If you want to use Java Object you need to create class which has a fields like database structure (fields has to same like contacts child).

    Users.class (Java Object):

    public class Users {
    
        private String name, number, email;
    
        public Users() {
        }
    
        public Users(String name, String number, String email) {
            this.name = name;
            this.number = number;
            this.email = email;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    }
    

    Then you can get your data as an object.

    private void sendMessage(DatabaseReference dbRef) {
            dbRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    SmsManager smsManager= SmsManager.getDefault();
                    for (DataSnapshot dsp: snapshot.getChildren()){
                        Users user = dsp.getValue(Users.class);
                        String phoneNumber = user.getNumber();
                        String message="I need help";
                        smsManager.sendTextMessage(phoneNumber,null,message,null,null);
                    }
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                }
            });
    }
    

    Both sendMessage() methods are getting data from database according to given DatabaseReference and using your code for sending message.