片段不会显示多个项目

时间:2018-09-10 14:22:22

标签: java android firebase android-fragments

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

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

片段类

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

        }

    }
}

1 个答案:

答案 0 :(得分:0)

正如我在您的代码中看到的那样,您正在使用Firebase-UI库的旧版本,其中由于存在错误,您明确不需要使用固定大小。为了在您的RecyclerView中显示数据,请删除/注释以下代码行:

convList.setHasFixedSize(true);

请参阅here更多信息。