0

Hello I'm trying to show the data's that i have inputted in my activity, but the problem is i don't have any idea how to, I'm at lost on the data snapshot one. I'm trying to like only show the name and email

My database Database

my code and progress so far

public class AdminActivity extends AppCompatActivity {
private DrawerLayout mdrawerl;
private ActionBarDrawerToggle mtoggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin);
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            collectName((Map<String,Object>)dataSnapshot.getValue());
        }
        private void collectName(Map<String, Object> value) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }                         
    });

reference that i used How to get all child's data in firebase database?

Yamiess
  • 251
  • 4
  • 15

1 Answers1

2

1- Create a model class that will hold the objects of user. In your case it would be

public class User {
private String email;
private String name;
private int type;

public User() {
}

public User(String email, String name, int type) {
    this.email= email;
    this.name= name;
    this.type= type;
}
}

2 - Create a ArrayList that will hold items of User

ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<User> userList = new ArrayList();
            for (DataSnapshot child :
                            dataSnapshot.getChildren()) {
                User user = child.getValue(User.class);
                userList.add(user )
            }
           //Here you have all user you can pass ```userList``` to your method furthur
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }                         
    });

Hope this will help you.

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
  • Hello can i ask what do you mean by "Here you have all user you can pass ```userList``` to your method furthur" ? – Yamiess Feb 13 '18 at 12:55
  • what i meant there, was that you will have all the data that you have under ```User``` node into ```userList``` you can pass userList to any of your method as param and perform your own use-case to it. – Zeeshan Shabbir Feb 13 '18 at 13:01
  • Oh Hello sir thanks for replying, umm sorry to say this but i don't understand :(( if possible can you provide me some samples? – Yamiess Feb 13 '18 at 13:05
  • What you don't understand? – Zeeshan Shabbir Feb 14 '18 at 05:18