我正在创建类似WhatsApp的社交应用。在联系人选项卡中,用户可以单击联系人并转到ChatActivity。我的问题是1。我看到的是Firebase数据库中的用户ID,而不是用户名显示在个人资料图像旁边的顶部和最后一次看到的上方。 2消息不显示。在ChatActivity中,用户可以看到来自接收者和发送者的消息以及其他人的个人资料图像。
当我单击“联系人”选项卡中的联系人时,它会将我发送到ChatActivity,但聊天记录不存在,并且显示用户ID而不是用户名。有人可以帮我吗?
ContactsFragment.Java:
private View ContactsView;
private RecyclerView myContactsList;
private DatabaseReference ContacsRef, UsersRef;
private FirebaseAuth mAuth;
private String currentUserID;
public ContactsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ContactsView = inflater.inflate(R.layout.fragment_contacts, container, false);
myContactsList = (RecyclerView) ContactsView.findViewById(R.id.contacts_list);
myContactsList.setLayoutManager(new LinearLayoutManager(getContext()));
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
ContacsRef = FirebaseDatabase.getInstance().getReference().child("Contacts").child(currentUserID);
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
return ContactsView;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions options =
new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(ContacsRef, Contacts.class)
.build();
FirebaseRecyclerAdapter<Contacts, ContactsViewHolder> adapter
= new FirebaseRecyclerAdapter<Contacts, ContactsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final ContactsViewHolder holder, final int position, @NonNull Contacts model) {
final String userIDs = getRef(position).getKey();
final String profileName = getRef(position).getKey();
final String userImage = getRef(position).getKey();
UsersRef.child(userIDs).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
if (dataSnapshot.child("userState").hasChild("state")) {
String state = (dataSnapshot.child("userState").child("state")).getValue().toString();
String date = (dataSnapshot.child("userState").child("date")).getValue().toString();
String time = (dataSnapshot.child("userState").child("time")).getValue().toString();
if (state.equals("online")) {
holder.onlineIcon.setVisibility(View.VISIBLE);
} else if (state.equals("offline")) {
holder.onlineIcon.setVisibility(View.INVISIBLE);
}
} else {
holder.userStatus.setText("offline");
}
if (dataSnapshot.hasChild("image")) {
String userImage = dataSnapshot.child("image").getValue().toString();
String profileName = dataSnapshot.child("name").getValue().toString();
String profileStatus = dataSnapshot.child("status").getValue().toString();
holder.userName.setText(profileName);
holder.userStatus.setText(profileStatus);
Picasso.get().load(userImage).placeholder(R.drawable.profile_image).into(holder.profileImage);
} else {
String profileName = dataSnapshot.child("name").getValue().toString();
String profileStatus = dataSnapshot.child("status").getValue().toString();
holder.userName.setText(profileName);
holder.userStatus.setText(profileStatus);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getContext(), ChatActivity.class);
i.putExtra("visit_user_id", currentUserID);
i.putExtra("visit_user_name", profileName);
i.putExtra("visit_image", userImage);
startActivity(i);
}
});
}
@NonNull
@Override
public ContactsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.users_display_layout, viewGroup, false);
ContactsViewHolder viewHolder = new ContactsViewHolder(view);
return viewHolder;
}
};
myContactsList.setAdapter(adapter);
adapter.startListening();
}
public static class ContactsViewHolder extends RecyclerView.ViewHolder {
TextView userName, userStatus;
CircleImageView profileImage;
ImageView onlineIcon;
public ContactsViewHolder(@NonNull View itemView) {
super(itemView);
userName = itemView.findViewById(R.id.user_profile_name);
userStatus = itemView.findViewById(R.id.user_status);
profileImage = itemView.findViewById(R.id.users_profile_image);
onlineIcon = (ImageView) itemView.findViewById(R.id.user_online_status);
}
}