“我为Android设计了一个聊天应用程序,我的代码有问题,发生的事情是用户重复,请如何纠正我的代码?”
这是有问题的代码,请帮助我修复此重复的用户,这是什么问题?请帮助我
package com.developer.fireone.finga.Fragment;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import com.developer.fireone.finga.Adapter.UserAdapter;
import com.developer.fireone.finga.Adapter.UserChatAdapter;
import com.developer.fireone.finga.Model.Chat;
import com.developer.fireone.finga.Model.ChatList;
import com.developer.fireone.finga.Model.User;
import com.developer.fireone.finga.Notifications.Token;
import com.developer.fireone.finga.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.Query;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.iid.FirebaseInstanceId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class ChatsFragment extends Fragment {
private RecyclerView recyclerView;
private UserChatAdapter userChatAdapter;
private List <User> mUsers, newUserArrayList;
FirebaseUser fuser;
DatabaseReference reference;
private List<String> usersList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chats, container, false);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
fuser= FirebaseAuth.getInstance().getCurrentUser();
usersList= new ArrayList<>();
reference = FirebaseDatabase.getInstance().getReference("Chats");
reference.keepSynced(true);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
usersList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Chat chat = snapshot.getValue(Chat.class);
if(chat.getSender().equals(fuser.getUid())) {
usersList.add(chat.getReceiver());
}
if(chat.getReceiver().equals(fuser.getUid())) {
usersList.add(chat.getSender());
}
}
readChats();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
updateToken(FirebaseInstanceId.getInstance().getToken());
return view;
}
private void readChats() {
mUsers = new ArrayList<>();
//Create a new list object
newUserArrayList = new ArrayList<>();
reference= FirebaseDatabase.getInstance().getReference("Users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
for(String id : usersList){
if(user.getId().equals(id)){
if(mUsers.size() != 0){
for(User user1 : mUsers){
if(!user.getId().equals(user1.getId())){
newUserArrayList.add(user);
break;
}
}
}else{
newUserArrayList.add(user);
}
}
}
}
userChatAdapter = new UserChatAdapter(getContext(), newUserArrayList, true);
recyclerView.setAdapter(userChatAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
复制图片:enter image description here 图片Firebase:enter image description here 我希望用户在发送或接收消息时出现在聊天框中一次