When I click on different person Chat room the app opening the same person chat

I’m facing a problem please check find the problem: When I click on a different chat room it opening the same chat room as you can see below: enter image description here

Here is the Chatsfragment.java (Left side screen)

public class ChatsFragment extends Fragment{

private RecyclerView recyclerView;

private FriendsAdapter userAdapter;
private List<Users> mUsers;

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.ChatsFragment_recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));


    usersList = new ArrayList<>();

    fuser = FirebaseAuth.getInstance().getCurrentUser();
    reference = FirebaseDatabase.getInstance().getReference("Chats");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            usersList.clear();

            for(DataSnapshot snapshot:dataSnapshot.getChildren()){

                Chat chat=snapshot.getValue(Chat.class);

                assert chat != null;
                if (chat.getSender().equals(fuser.getUid())) {

                    usersList.add(chat.getReceiver());

                }

                if (chat.getReceiver().equals(fuser.getUid())) {

                    usersList.add(chat.getSender());

                }

            }


            Set<String> hashSet = new HashSet<String>(usersList);
            usersList.clear();
            usersList.addAll(hashSet);
            readChats();


        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    UpdateToken();
    return view;
}

private void UpdateToken() {
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    String refreshToken = FirebaseInstanceId.getInstance().getToken();
    Token token = new Token(refreshToken);
    FirebaseDatabase.getInstance().getReference("Tokens").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(token);
}

private void readChats() {

    mUsers=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()){

                Users user=snapshot.getValue(Users.class);

                for(String id:usersList)
                {

                    assert user != null;
                    if (user.getUser_id().equals(id)) {

                        mUsers.add(user);

                    }

                }

            }

            userAdapter=new FriendsAdapter(getContext(),mUsers,true);
            recyclerView.setAdapter(userAdapter);


        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}}

Here is the MessageActivity.java(Right side screen)

public class MessageActivity extends AppCompatActivity {

CircleImageView profile_image;
TextView username,fullname;

FirebaseUser fuser;
DatabaseReference reference;

private Toolbar toolbar;
Intent intent;
boolean notify = false;

TextView btn_send;
EditText text_send;

MessageAdapter messageAdapter;
List<Chat> mchat;

RecyclerView recyclerView;
String userid;

private APIService apiService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message);

    toolbar = findViewById(R.id.MessageActivity_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    username = (TextView)findViewById(R.id.MessageActivity_userName);
    fullname = (TextView)findViewById(R.id.MessageActivity_fullname);
    profile_image = (CircleImageView)findViewById(R.id.MessageActivity_user_img);
    btn_send =findViewById(R.id.MessageActivity_btn_send);
    text_send = findViewById(R.id.MessageActivity_text_send);

    apiService = Client.getClient("https://fcm.googleapis.com/").create(APIService.class);

    recyclerView = findViewById(R.id.MessageActivity_recyclerView);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    linearLayoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(linearLayoutManager);

    intent = getIntent();
    userid = intent.getStringExtra("userid");

    fuser = FirebaseAuth.getInstance().getCurrentUser();
    reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Users user = dataSnapshot.getValue(Users.class);
            username.setText(user.getUsername());
            fullname.setText(user.getFullName());
            Glide.with(MessageActivity.this)
                    .load(user.getProfilePhoto())
                    .into(profile_image);
            readMesagges(fuser.getUid(), userid, user.getProfilePhoto());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    btn_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            notify = true;
            String msg = text_send.getText().toString();
            if (!msg.equals("")){
                sendMessage(fuser.getUid(), userid, msg);
            } else {
                Toast.makeText(MessageActivity.this, "You can't send empty message", Toast.LENGTH_SHORT).show();
            }
            text_send.setText("");
            closeKeyboard();
        }
    });

}

private void sendMessage(String sender, final String receiver, String message){

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("sender", sender);
    hashMap.put("receiver", receiver);
    hashMap.put("message", message);
    hashMap.put("isseen", false);

    reference.child("Chats").push().setValue(hashMap);

    final String msg = message;
    reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Users user = dataSnapshot.getValue(Users.class);
            if (notify) {
                sendNotifiaction(receiver, user.getUsername(), msg);
            }
            notify = false;
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

private void sendNotifiaction(String receiver, final String username, final String message) {

    DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
    Query query = tokens.orderByKey().equalTo(receiver);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Token token = snapshot.getValue(Token.class);
                Data data = new Data(fuser.getUid(), R.mipmap.ic_launcher, username+":"+message, "New Message",
                        userid);

                NotificationSender sender = new NotificationSender(data, token.getToken());

                apiService.sendNotification(sender).enqueue(new Callback<MyResponse>() {
                    @Override
                    public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
                        if (response.code() == 200){
                            if (response.body().success != 1){
                                Toast.makeText(MessageActivity.this, "Failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<MyResponse> call, Throwable t) {

                    }
                });
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

private void readMesagges(final String myid, final String userid, final String imageurl){
    mchat = new ArrayList<>();

    reference = FirebaseDatabase.getInstance().getReference("Chats");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            mchat.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Chat chat = snapshot.getValue(Chat.class);
                if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid) ||
                        chat.getReceiver().equals(userid) && chat.getSender().equals(myid)){
                    mchat.add(chat);
                }
                messageAdapter = new MessageAdapter(MessageActivity.this, mchat, imageurl);
                recyclerView.setAdapter(messageAdapter);

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

private void closeKeyboard(){
    View view = this.getCurrentFocus();
    if(view != null){
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
} }

Here is the FriendsAdapter.java

    public FriendsAdapter(Context mcontext, List<Users> muser, boolean ischat) {
        this.mcontext = mcontext;
        this.muser = muser;
        this.ischat = ischat;
    }

    @NonNull
    @Override
    public FriendsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mcontext).inflate(R.layout.friends_single_layout,parent,false);
        return new FriendsAdapter.ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull FriendsAdapter.ViewHolder holder, int position) {

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        users = muser.get(position);
        holder.username.setText(users.getFullName());
//        holder.fullname.setText(users.getFullName());
        Glide.with(mcontext)
                .load(users.getProfilePhoto())
                .into(holder.profileimage);

        if (ischat){
            lastMessage(users.getUser_id(), holder.last_msg);
        } else {
            holder.last_msg.setText(users.getFullName());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mcontext, MessageActivity.class);
                intent.putExtra("userid",users.getUser_id());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mcontext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return muser.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView username,last_msg;
        public CircleImageView profileimage;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            username = itemView.findViewById(R.id.FriendSingle_userName);
            last_msg = itemView.findViewById(R.id.FriendSingle_lastMsg);
            profileimage = itemView.findViewById(R.id.FriendSingle_user_img);
        }
    }

    //check for last message
    private void lastMessage(final String userid, final TextView last_msg){
        theLastMessage = "default";
        final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    Chat chat = snapshot.getValue(Chat.class);
                    if (firebaseUser != null && chat != null) {
                        if (chat.getReceiver().equals(firebaseUser.getUid()) && chat.getSender().equals(userid) ||
                                chat.getReceiver().equals(userid) && chat.getSender().equals(firebaseUser.getUid())) {
                            theLastMessage = chat.getMessage();
                        }
                    }
                }

                switch (theLastMessage){
                    case  "default":
                        last_msg.setText(users.getFullName());
                        break;

                    default:
                        last_msg.setText(theLastMessage);
                        break;
                }

                theLastMessage = "default";
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }}

Again, the problem is whenever user click on recyclerView item it shows the same chat room, please find the errer and let me know as soon as possible.

Thanks for your time

Answer

From what you’ve posted (there seems to be part of FriendsAdapter.java missing), it looks like the issue is in your FriendsAdapter. You have probably defined the users variable at the Adapter level, but it should be defined at the ViewHolder level or just used as a local variable.

Your are calling users = muser.get(position) in your bindViewHolder method so if users is defined at adapter level when your onClickListener is fired the users variable will be set to the user of the last cell, instead of the user object associated with the cell clicked.

So, remove the definition of users from adapter level (probably looks like this):

User users;

And either use users as a local variable inside the bindViewHolder method:

User users = muser.get(position);

Or put the definition inside your ViewHolder:

public class ViewHolder extends RecyclerView.ViewHolder {

    public User users;
    public TextView username,last_msg;
    public CircleImageView profileimage;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        username = itemView.findViewById(R.id.FriendSingle_userName);
        last_msg = itemView.findViewById(R.id.FriendSingle_lastMsg);
        profileimage = itemView.findViewById(R.id.FriendSingle_user_img);
    }
}