android 当用户发来新消息时,他的名字的用户列表不首先出现吗?

yqlxgs2m  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(116)

enter image description here
用户适配器和用户列表活动我们附加。我想用户最后的消息显示在列表中的特殊颜色,如whatsapp。任何人帮助我。我不知道该怎么做。
用户列表和用户适配器是用户..多次尝试我不能这样做
`

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.chatviewholder> {

Context context;
    ArrayList<User> users;

    public UserListAdapter(Context context, ArrayList<User> users) {
        this.context = context;
        this.users = users;
    }

    @NonNull
    @Override
    public chatviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(context).inflate(R.layout.sample_chatting, parent, false);

        return new chatviewholder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull chatviewholder holder, int position) {

        User user = users.get(position);
        String senderID = FirebaseAuth.getInstance().getUid();
        String senderRoom = senderID + user.getUid();
        FirebaseDatabase.getInstance().getReference()
                .child("chats")
                .child(senderRoom)
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if (snapshot.exists()) {
                        String lastMsg = snapshot.child("lastMsg").getValue(String.class);
                        long time = snapshot.child("lastMsgTime").getValue(Long.class);
                            SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");
                                holder.binding.msgTime.setText(dateFormat.format(new Date(time)));

                        holder.binding.lastmess.setText(lastMsg);
                           
                        
                        } else {

                            holder.binding.lastmess.setText("Tap to chat");
                        }
                    }

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

                    }
                });

     //   holder.binding.msgTime.setText(user.getDate());

        holder.binding.username.setText(user.getName());
    //   Glide.with(context).load(user.getProfileImage()).placeholder(R.drawable.avatar).into(holder.binding.profile);
       //Picasso.get().load(user.getProfileImage()).placeholder(R.drawable.avatar).into(holder.binding.profile);

       holder.itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(context, chatbox.class);
              intent.putExtra("name",user.getName() );//"Admin"
             // intent.putExtra("image", user.getProfileImage());
              intent.putExtra("uid",user.getUid() );
              intent.putExtra("token",user.getToken() );
              // user Specific Id

               context.startActivity(intent);
           }
       });

    }

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

    public class chatviewholder extends RecyclerView.ViewHolder {

        SampleChattingBinding binding;
        public chatviewholder(@NonNull View itemView) {
            super(itemView);
            binding = SampleChattingBinding.bind(itemView);

        }
    }
}

`
userlist '中使用的活动

public class chatList extends AppCompatActivity {
    ChatlistActivityBinding binding;
    FirebaseDatabase database;
    ArrayList<User> users;
    UserListAdapter userListAdapter;
    User userModel;
    DatabaseReference reference;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ChatlistActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        database = FirebaseDatabase.getInstance();
        reference = FirebaseDatabase.getInstance().getReference("chats/hj6HUpdfQNNoNZuMPX3ZPueo5HQ2ppLQWoV78Xe1yu4qWTaXhi1LKz13");
        FirebaseMessaging.getInstance().getToken().addOnSuccessListener(new OnSuccessListener<String>() {
            @Override
            public void onSuccess(String s) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("token", s);
                database.getReference()
                        .child("users")
                        .child(FirebaseAuth.getInstance().getUid())
                        .updateChildren(map);

            }
        });

        users = new ArrayList<>();

       // binding.recyclerView.showShimmerAdapter();

      refreshData();




    }

    private  void refreshData(){
        database.getReference().child("users").child(FirebaseAuth.getInstance().getUid())
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        userModel = snapshot.getValue(User.class);
                    }

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

                    }
                });
        userListAdapter = new UserListAdapter(this, users);
        binding.recyclerView.setAdapter(userListAdapter);
        database.getReference().child("users").addValueEventListener(new ValueEventListener() {
            //
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                users.clear();
                for(DataSnapshot snapshot1 : snapshot.getChildren()){
                    User user = snapshot1.getValue(User.class);
                    if(!user.getUid().equals(FirebaseAuth.getInstance().getUid()))

                        users.add(user);

                }

                //binding.recyclerView.hideShimmerAdapter();

                userListAdapter.notifyDataSetChanged();

            }

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

            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        String currentId = FirebaseAuth.getInstance().getUid();
        database.getReference().child("presence").child(currentId).setValue("Online");


    }

    @Override
    public void onPause() {
        super.onPause();
        String currentId = FirebaseAuth.getInstance().getUid();
        database.getReference().child("presence").child(currentId).setValue("Offline");
    }



    }

`
我希望当用户列表中的任何发送消息时,它首先以绿色显示。

hec6srdp

hec6srdp1#

当传递用户列表到适配器时,请反转用户列表,以便新添加的用户可以在顶部看到。如果您仍然面临问题,请在评论中让我知道。

相关问题