0

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.

enter image description here

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.