I have created an Android chat application which incorporates Firebase and created a fragment with a current users messages. The error occurs when a user sends a message to more than one person and the following image link shows how the first message is displayed whereas the most recent one is invisible. Does anyone know why this would be? I will include a few of my classes below.
My firebase is displayed as the following with messages containing first the user IDs, recipient IDs and then the unique message ID followed by the actual message and who sent it.
Fragment Class
public class ChatFragment extends Fragment {
private RecyclerView convList;
private FirebaseAuth auth;
private DatabaseReference userDatabase, messageDatabase;
private View view;
public ChatFragment() {
// Required empty public constructor
}
// Chat Fragment initialised
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_chat, container, false);
convList = (RecyclerView) view.findViewById(R.id.conv_list);
convList.setHasFixedSize(true);
convList.setLayoutManager(new LinearLayoutManager(getContext()));
auth = FirebaseAuth.getInstance();
String currentUser;
currentUser = auth.getCurrentUser().getUid();
messageDatabase = FirebaseDatabase.getInstance().getReference().child("Messages").child(currentUser);
messageDatabase.keepSynced(true);
userDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
userDatabase.keepSynced(true);
return view;
}
// Use FirebaseRecyclerAdapter to populate each user of which messages have been sent between, the latest message sent between each user displayed, ordering the list by most recent message and the user's online status
@Override
public void onStart() {
super.onStart();
Query conversationQuery = messageDatabase.orderByChild("time");
FirebaseRecyclerAdapter<UserConversation, ConvViewHolder> friendsRecyclerViewAdapter = new FirebaseRecyclerAdapter<UserConversation, ConvViewHolder>(
UserConversation.class,
R.layout.activity_user_list,
ConvViewHolder.class,
conversationQuery
) {
@Override
protected void populateViewHolder(final ConvViewHolder friendsViewHolder, final UserConversation friends, int i) {
final String user_id = getRef(i).getKey();
Query lastMessageQuery = messageDatabase.child(user_id).limitToLast(1);
// Retrieving data stored in Firebase for each message and its information
lastMessageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data = dataSnapshot.child("message").getValue().toString();
ConvViewHolder.setMessage(data);
String time = dataSnapshot.child("time").getValue().toString();
ConvViewHolder.setTime(time);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
userDatabase.child(user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
String online = dataSnapshot.child("online").getValue().toString();
friendsViewHolder.setName(userName);
friendsViewHolder.setUserImage(userThumb, getContext());
friendsViewHolder.setUserOnline(online);
friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), Messenger.class);
intent.putExtra("user_id", user_id);
intent.putExtra("user_name", userName);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Log.i("Activity Not Found", "Error");
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
convList.setAdapter(friendsRecyclerViewAdapter);
}
public static class ConvViewHolder extends RecyclerView.ViewHolder {
static View mView;
public ConvViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public static void setMessage(String message){
TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
userStatusView.setText(message);
}
public void setName(String name) {
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public void setUserImage(String thumb_image, Context ctx) {
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
public static void setTime (String time){
TextView test = (TextView) mView.findViewById(R.id.time_sent);
test.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", Long.parseLong(time)));
}
public void setUserOnline(String online) {
ImageView userOnlineView = (ImageView) mView.findViewById(R.id.user_single_online_icon);
if(online.equals("true")){
userOnlineView.setVisibility(View.VISIBLE);
} else {
userOnlineView.setVisibility(View.INVISIBLE);
}
}
}
As I can see in your code, you are using an older version of the
Firebase-UI
library, in which because of a bug, you explicitly don't need to use fixed size. In order to display the data in yourRecyclerView
, please remove/comment the following line of code:Please see here more informations.