The chats/messages are not appearing on my app's chat windows but when I have checked my firebase database it is showing there properly. Here is the code please look into it.
ChatWindow.java
package com.vortexv.vconnect;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.Date;
import de.hdodenhof.circleimageview.CircleImageView;
public class Chat_Window extends AppCompatActivity {
String receiver_Img, receiverName, RecieverUID, SenderUID;
CircleImageView profile;
TextView receiver_Name;
CardView send_btn;
EditText txt_msg;
FirebaseAuth firebaseAuth;
FirebaseDatabase database;
public static String senderImg;
public static String receiverImg;
String senderRoom, receiverRoom;
RecyclerView mmessagesAdapter;
ArrayList<msgModelclass> messageArrayList;
messagesAdapter messagesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_window);
database = FirebaseDatabase.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
receiverName = getIntent().getStringExtra("nameeee");
receiver_Img = getIntent().getStringExtra("receiverImg");
RecieverUID = getIntent().getStringExtra("uid");
messageArrayList = new ArrayList<>();
send_btn = findViewById(R.id.send_btnn);
txt_msg = findViewById(R.id.txt_msg);
profile = findViewById(R.id.chat_profileimg);
receiver_Name = findViewById(R.id.receiver_name);
mmessagesAdapter = findViewById(R.id.msgAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
mmessagesAdapter.setLayoutManager(linearLayoutManager);
messagesAdapter = new messagesAdapter(Chat_Window.this, messageArrayList);
mmessagesAdapter.setAdapter(messagesAdapter);
Picasso.get().load(receiver_Img).into(profile);
receiver_Name.setText(receiverName);
SenderUID = firebaseAuth.getUid();
senderRoom = SenderUID + RecieverUID;
receiverRoom = RecieverUID + SenderUID;
DatabaseReference reference = database.getReference().child("user").child(firebaseAuth.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists() && snapshot.child("profilepic").exists()) {
senderImg = snapshot.child("profilepic").getValue().toString();
receiverImg = receiver_Img;
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
DatabaseReference chatreference = database.getReference().child("user").child(senderRoom).child("messages");
chatreference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
messageArrayList.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
msgModelclass messages = dataSnapshot.getValue(msgModelclass.class);
messageArrayList.add(messages);
}
messagesAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
send_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = txt_msg.getText().toString();
if (message.isEmpty()) {
Toast.makeText(Chat_Window.this, "Enter The Message First", Toast.LENGTH_SHORT).show();
} else {
txt_msg.setText("");
Date date = new Date();
msgModelclass messages = new msgModelclass(message, SenderUID, date.getTime());
DatabaseReference senderRef = database.getReference().child("chats").child(senderRoom).child("messages");
DatabaseReference receiverRef = database.getReference().child("chats").child(receiverRoom).child("messages");
senderRef.push().setValue(messages).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
receiverRef.push().setValue(messages).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
}
});
}
});
}
}
});
}
}
messagesAdapter.java
package com.vortexv.vconnect;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.auth.FirebaseAuth;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class messagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
ArrayList<msgModelclass> messageAdapterArrayList;
int ITEM_SEND = 1;
int ITEM_RECEIVE = 2;
public messagesAdapter(Context context, ArrayList<msgModelclass> messageAdapterArrayList) {
this.context = context;
this.messageAdapterArrayList = messageAdapterArrayList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == ITEM_SEND) {
View view = LayoutInflater.from(context).inflate(R.layout.sender_layout,parent,false);
return new senderViewHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.receiver_layout,parent,false);
return new receiverViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
msgModelclass messages = messageAdapterArrayList.get(position);
if (holder.getClass() == senderViewHolder.class) {
senderViewHolder viewHolder = (senderViewHolder) holder;
viewHolder.msgtxt.setText(messages.getMessage());
Picasso.get().load(Chat_Window.senderImg).into(viewHolder.circleImageView);
} else {
receiverViewHolder viewHolder = (receiverViewHolder) holder;
viewHolder.msgtxt.setText(messages.getMessage());
Picasso.get().load(Chat_Window.receiverImg).into(viewHolder.circleImageView);
}
}
@Override
public int getItemCount() {
return messageAdapterArrayList.size();
}
@Override
public int getItemViewType(int position) {
msgModelclass messages = messageAdapterArrayList.get(position);
if (FirebaseAuth.getInstance().getCurrentUser().getUid().equals(messages.getSenderId())) {
return ITEM_SEND;
} else {
return ITEM_RECEIVE;
}
}
static class senderViewHolder extends RecyclerView.ViewHolder {
CircleImageView circleImageView;
TextView msgtxt;
senderViewHolder(@NonNull View itemView) {
super(itemView);
circleImageView = itemView.findViewById(R.id.profilerggg);
msgtxt = itemView.findViewById(R.id.msgsendertyp);
}
}
static class receiverViewHolder extends RecyclerView.ViewHolder {
CircleImageView circleImageView;
TextView msgtxt;
receiverViewHolder(@NonNull View itemView) {
super(itemView);
circleImageView = itemView.findViewById(R.id.pro);
msgtxt = itemView.findViewById(R.id.recivertextset);
}
}
}
msgModelClass.java
package com.vortexv.vconnect;
public class msgModelclass {
String message;
String senderId;
public msgModelclass() {
}
public msgModelclass(String message, String senderId, long timeStamp) {
this.message = message;
this.senderId = senderId;
this.timeStamp = timeStamp;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getSenderId() {
return senderId;
}
public void setSenderId(String senderId) {
this.senderId = senderId;
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
long timeStamp;
}
What I have tried:
I have checked the logcat but there is no problem in that. Please help me. If you still facing problem to solve this error, you can tell me what you want to fix this because I am still learning app development so don't know much about it.