Reputation: 583
In my ChatListActivity, I am getting emails of users and showing them in a listView using Firebase. There is value in Firebase because when the arraylist is showing its data when I use log, but isn't showing in ListView. Please help.
public class ChatListActivity extends AppCompatActivity {
ListView userListView;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> users = new ArrayList<>();
FirebaseAuth mAuth;
DatabaseReference databaseReference;
Button signOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_list);
mAuth = FirebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference();
userListView = findViewById(R.id.userListView);
signOut = findViewById(R.id.signOut);
databaseReference.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
String email = dataSnapshot.child("email").getValue().toString();
if (!email.equals(FirebaseAuth.getInstance().getCurrentUser().getEmail())){
users.add(email);
Log.i("Log", Arrays.toString(new ArrayList[]{users}));
}
}
arrayAdapter = new ArrayAdapter<>(ChatListActivity.this, android.R.layout.simple_list_item_1, users);
userListView.setAdapter(arrayAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(ChatListActivity.this, "Failed to load users", Toast.LENGTH_SHORT).show();
}
});
signOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.signOut();
Intent intent = new Intent(ChatListActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
Here is my log.
Upvotes: 0
Views: 36
Reputation: 1368
Do set an empty adapter before network call, like this
arrayAdapter = new ArrayAdapter<>(ChatListActivity.this, android.R.layout.simple_list_item_1, users);
userListView.setAdapter(arrayAdapter);
after you receive original response, reset the adapter with new data. That should work.
now "users" will have some data
arrayAdapter = new ArrayAdapter<>(ChatListActivity.this, android.R.layout.simple_list_item_1, users);
userListView.setAdapter(arrayAdapter);
Upvotes: 1