0

I have a method for a chat app, everything seems to run with no errors but every time I try to type something nothing pops up.

When users log in, the onActivityResult shows the success message:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SIGN_IN_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Snackbar.make(activity_main, "Success. Welcome.", Snackbar.LENGTH_SHORT).show();
        }
        else {
            Snackbar.make(activity_main, "Error", Snackbar.LENGTH_SHORT).show();
            finish();
        }
    }
}

However, it does not show my onCreate welcome message after:

if(FirebaseAuth.getInstance().getCurrentUser() == null) {
    startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_REQUEST_CODE);
}
else {
    Snackbar.make(activity_main, "Welcome " + FirebaseAuth.getInstance().getCurrentUser().getEmail(), Snackbar.LENGTH_SHORT);
    //Load content
    displayChat();
}

When a message is typed, nothing shows up too. Am I querying incorrectly?

private void displayChat() {

    ListView listOfMessage = findViewById(R.id.list_of_message);

    Query query = FirebaseDatabase.getInstance().getReference();
    FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
            .setLayout(R.layout.list_item)
            .setQuery(query, Chat.class)
            .build();

    adapter = new FirebaseListAdapter<Chat>(options) {
        @Override
        protected void populateView(View v, Chat model, int position) {
            //Get reference to the views of list_item.xml
            TextView messageText, messageUser, messageTime;
            messageText = v.findViewById(R.id.message_text);
            messageUser = v.findViewById(R.id.message_user);
            messageTime = v.findViewById(R.id.message_time);

            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
        }
    };
    listOfMessage.setAdapter(adapter);
}

My database seems to be fine, with no errors on input:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
compsciman
  • 349
  • 3
  • 17

1 Answers1

0

You need to get the refrence for a database and then query on it:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

Query on it something like this, based on you requirement :-

Query query = reference.child("Chat_room");
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            // dataSnapshot is the "chat_room" node with all children 
            for (DataSnapshot issue : dataSnapshot.getChildren()) {
                // do something with the individual "issues"
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Note :- make sure your database has been created , the structure in which you are saving the chat messages to the databse is important for way you want to retrieve and display the chats.

Kaveri
  • 1,060
  • 2
  • 12
  • 21