I am building a chat app.
I am trying to display the messages from Firebase Firestore to a RecyclerView.
The problem is that, in my Adapter, only the constructor executes.
Here is my ChatActivity:
public class ChatActivity extends AppCompatActivity {
public RecyclerView recyclerView;
public ChatAdapter chatAdapter;
public ArrayList<MessageChat> allMessages = new ArrayList<>();
public String userID;
public FirebaseFirestore db;
public FirebaseAuth mAuth;
public MessageChat message;
public Bundle b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
db = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
userID = FirebaseAuth.getInstance().getUid();
Intent intent = getIntent();
b = intent.getExtras();
String matchName = b.getString("matchName");
recyclerView = findViewById(R.id.recyclerViewChatMessages);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
chatAdapter = new ChatAdapter(this, allMessages, userID);
recyclerView.setAdapter(chatAdapter);
displayMessages(b.getString("matchID"));
}
public void displayMessages(String matchID) {
db.collection("messages")
.whereIn("receiverUserID", Arrays.asList(userID, matchID))
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
return;
}
allMessages.clear();
for (QueryDocumentSnapshot document : value) {
message = new MessageChat();
if (document.getString("senderUserID").equals(userID) || document.getString("senderUserID").equals(matchID)) {
message.content = document.getString("content");
message.date = (long) document.get("date");
message.receiverUserID = document.getString("receiverUserID");
message.senderUserID = document.getString("senderUserID");
allMessages.add(message);
}
}
// here my messages are well added into the list
chatAdapter.notifyDataSetChanged();
}
});
}
And my ChatAdapter
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context currentContext;
private ArrayList<MessageChat> listOfMessages;
private String userID;
private static final int LAYOUT_SENT = 1;
private static final int LAYOUT_RECEIVED = 0;
public ChatAdapter(Context cContext, ArrayList<MessageChat> cMessages, String cID) {
// only this constructor is called, not the methods bellow
this.currentContext = cContext;
this.listOfMessages = cMessages;
this.userID = cID;
}
@Override
public int getItemViewType(int position){
if (listOfMessages.get(position).senderUserID.equals(userID)){
return LAYOUT_SENT;
}
else {
return LAYOUT_RECEIVED;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == LAYOUT_SENT){
View view = LayoutInflater.from(currentContext).inflate(R.layout.message_item_sent, parent, false);
return new ChatAdapter.SenderMessageViewHolder(view);
}
else {
View view = LayoutInflater.from(currentContext).inflate(R.layout.message_item_received, parent, false);
return new ChatAdapter.ReceiverMessageViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == LAYOUT_SENT){
((SenderMessageViewHolder) holder).sentMessage.setText(listOfMessages.get(position).content);
}
else {
((ReceiverMessageViewHolder) holder).receivedMessage.setText(listOfMessages.get(position).content);
}
}
@Override
public int getItemCount() {
return listOfMessages.size();
}
public static class SenderMessageViewHolder extends RecyclerView.ViewHolder {
public TextView sentMessage;
public TextView sentDate;
public SenderMessageViewHolder(@NonNull View itemView) {
super(itemView);
sentMessage = itemView.findViewById(R.id.textViewSentMessage);
sentDate = itemView.findViewById(R.id.textViewSentDate);
}
}
public static class ReceiverMessageViewHolder extends RecyclerView.ViewHolder {
public TextView receivedMessage;
public TextView receivedDate;
public ReceiverMessageViewHolder(@NonNull View itemView) {
super(itemView);
receivedMessage = itemView.findViewById(R.id.textViewReceivedMessage);
receivedDate = itemView.findViewById(R.id.textViewReceivedDate);
}
}
}
My messages are correctly added into the list when I call displayMessages, but I cannot display them.
Do you know how to make the RecyclerView work ?
CodePudding user response:
try to call chatAdapter.notifyDataSetChanged() after recyclerView.setAdapter(chatAdapter);
CodePudding user response:
Actually, in Your the ChatActivity, the messages list is passing to the adapter but you are actually not setting the list to the Layout manager. Try using LinearLayoutManager to display the message.
https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager?authuser=1
