片段未显示多个Firebase

时间:2019-05-21 19:28:44

标签: java android firebase

我创建了一个包含Firebase的Android聊天应用程序,并使用当前用户消息创建了一个片段。当用户向多个人发送消息时出现错误,并且以下图像链接显示了第一个用户的显示方式,而我向其发送消息的最新用户是不可见的。有谁知道为什么会这样吗?我将在下面包括我的一些课程。

我的firebase显示如下,其中的消息首先包含用户ID,收件人ID,然后是唯一的消息ID,然后是实际消息以及发送者。

感谢您的帮助。

https://i.stack.imgur.com/TrFUg.png

在下面的链接上,您将看到两个选项卡,第一个不显示,第二个显示。两条消息都发送给不同的收件人。

https://i.stack.imgur.com/M5LXa.png

片段类

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);
            }

        }
    }
}

0 个答案:

没有答案