I am using the post comment system in android, I want to create a chat option upon clicking the button only among the post author and comment author only, if anyone else clicks it should not open the chat room.
I am using the below code for the same purpose, but it is not working the way required. you can refer the idea of chat system in stackoverflow, the similar thing I want. the below code is in adapter class linking to the button on the recyclerview item. I am the PostAuthorId and CommentAuthorId are already confirmed, I have to just set the condition if the login user is the same as one of them.
i am using firestore database for the same
if any more information is required do let me know.
commentsViewHolder.chat.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View view) {
if(PostAuthorID == mAuth.getUid() || CommentAuthorId == mAuth.getUid())
{
Toast.makeText(CommentActivity.this, "Hello", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CommentActivity.this, ChatRoomActivity.class);
intent.putExtra(ChatRoomActivity.CHAT_ROOM_ID, ChatRoomId);
intent.putExtra(ChatRoomActivity.CHAT_ROOM_NAME, ChatRoomName);
startActivity(intent);
}
else
{
Toast.makeText(CommentActivity.this, "You are not authenticated", Toast.LENGTH_SHORT).show();
}
}
});
Probably the problem is that ==
won't work for strings.
Use PostAuthorID.equals(mAuth.getUid())
to compare strings, not the ==
operator. mAuth.getUid()
returns a String and I am assuming that PostAuthorID
is a String too
Like:
if(PostAuthorID.equals(mAuth.getUid()) || CommentAuthorId.equals(mAuth.getUid())){
....
} else {
....
}
Remember:
==
operator only compares object references, while theString.equals()
method compares both String's values!