Reputation: 221
I have an activity in my application where I want to show a list of all authenticated users. For that I am using a listview and an arrayadapter.
Problem: When I query my users and shows them on the listview, the list shows two rows both containg "null". I don't know why.
final String userId = user.getUid();
database = FirebaseDatabase.getInstance().getReference(sharedPreferences.getString("school",null)).child("users").child(userId);
userList = (ListView) findViewById(R.id.highscoreList);
final Query users = database;
users.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
userArray.add(data.child("username").getValue()+ "") ;
}
ArrayAdapter adapter = new ArrayAdapter(StudentListActivity.this, android.R.layout.simple_list_item_1, userArray);
userList.setAdapter(adapter);
}
};
Database
Upvotes: 1
Views: 148
Reputation: 598728
As far as I can see, your database
already points to a specific user in a specific school. For example the path /SomeSchoolName/Users/LAGbzHr...
contains the properties for a single user. In that case the for
loop in your listener is not needed, and will in fact cause you to go one level too deep into your JSON.
This should work better with the data structure you shared:
users.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userArray.add(dataSnapshot.child("username").getValue(String.class)) ;
ArrayAdapter adapter = new ArrayAdapter(StudentListActivity.this, android.R.layout.simple_list_item_1, userArray);
userList.setAdapter(adapter);
}
Upvotes: 1