如何使我的回收站适配器检索发送到Firebase数据库的消息?

时间:2019-04-05 00:43:50

标签: java android xml firebase firebase-realtime-database

我无法使聊天显示在回收站适配器中,我已经在Firebase数据库中,并且还附加了Firebase回收站适配器

enter image description here

我尝试了一些使回收站适配器显示聊天但无法显示适配器的方法

private void DisplayReceiverInformation() {

    receiverName.setText(messageReceiverName);

    rootReference.child("Users").child(messageReceiverID).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                //retrieve from db
                final String userName = dataSnapshot.child("fullName").getValue().toString();
                final String profileImage = dataSnapshot.child("profileImage").getValue().toString();

                //set values
                receiverName.setText(userName);
                Picasso.get().load(profileImage).placeholder(R.drawable.profile_image_placeholder).into(receiverProfileImage);
            }
        }

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

        }
    });
}

\\\

//initialize the above variables
private void InitializeFields() {

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //connect chat custom bar to chat activity
    ActionBar actionBar = getSupportActionBar();
    Objects.requireNonNull(actionBar).setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowCustomEnabled(true);
    LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View action_bar_view = layoutInflater.inflate(R.layout.chat_custom_toolbar, null);
    actionBar.setCustomView(action_bar_view);

    receiverName = findViewById(R.id.custom_profile_name);
    receiverProfileImage = findViewById(R.id.custom_profile_image);

    sendMessageButton = findViewById(R.id.send_message_button);
    userMessageInput = findViewById(R.id.input_message);

   // messageAdapter = new MessagesAdapter(ChatActivity.this,)
    messageAdapter = new MessagesAdapter( messageList);
    userMessagesList = (RecyclerView) findViewById(R.id.messages_list_of_users);
    linearLayoutManager = new LinearLayoutManager(this);
    userMessagesList.setHasFixedSize(true);
    userMessagesList.setLayoutManager(linearLayoutManager);
    userMessagesList.setAdapter(messageAdapter);


}

\\\

@Override
protected void onStart() {

    super.onStart();

    rootReference.child("Messages").child(messageSenderID).child(messageReceiverID)
            .addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                    Messages messages = dataSnapshot.getValue(Messages.class);

                    messageList.add(messages);


                    messageAdapter.notifyDataSetChanged();

                }

                @Override
                public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

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

                }
            });


}

\\\
MessagesAdapter.java

package com.MwandoJrTechnologies.the_smart_parent.Chats;



public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.MessageViewHolder> {

    private List<Messages> userMessagesList;
    private FirebaseAuth mAuth;
    private DatabaseReference usersDatabaseReference;

    public MessagesAdapter(List<Messages> userMessagesList) {
        this.userMessagesList = userMessagesList;
    }

    public class MessageViewHolder extends RecyclerView.ViewHolder {

        public TextView senderMessageText;
        public TextView receiverMessageText;
        public CircleImageView receiverProfileImage;

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

            senderMessageText = (TextView) itemView.findViewById(R.id.sender_message_text_view);
            receiverMessageText = (TextView) itemView.findViewById(R.id.receiver_message_text_view);
            receiverProfileImage = (CircleImageView) itemView.findViewById(R.id.receiver_message_profile_image);
        }
    }

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

        mAuth = FirebaseAuth.getInstance();

        return new MessageViewHolder(view);
    }

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

        //get senders ID
        String messageSenderID = mAuth.getCurrentUser().getUid();
        Messages messages = userMessagesList.get(position);

        //get ID of the receiver
        String fromUserID = messages.getFrom();
        //now get type of message whether text or image
        String fromMessageType = messages.getType();


        //retrieve receivers profile image
        usersDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(fromUserID);
        usersDatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                if (dataSnapshot.hasChild("profileImage")){

                    String receiverImage = dataSnapshot.child("profileImage").getValue().toString();
                    Picasso.get().load(receiverImage).placeholder(R.drawable.profile_image_placeholder)
                            .into(holder.receiverProfileImage);
                }
            }

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

            }
        });

        if (fromMessageType.equals("text")){
            holder.receiverMessageText.setVisibility(View.INVISIBLE);
            holder.receiverProfileImage.setVisibility(View.INVISIBLE);

                    //display the message to the sender and to the receiver
            //for the receiver
            if (fromUserID.equals(messageSenderID)) {
                holder.senderMessageText.setBackgroundResource(R.drawable.sender_text_message_background);
                holder.senderMessageText.setTextColor(Color.WHITE);
                holder.senderMessageText.setGravity(Gravity.LEFT);
                holder.senderMessageText.setText(messages.getMessage());

                //for receiver
            } else {
                holder.senderMessageText.setVisibility(View.INVISIBLE);

                holder.receiverMessageText.setVisibility(View.VISIBLE);
                holder.receiverProfileImage.setVisibility(View.VISIBLE);

                holder.receiverMessageText.setBackgroundResource(R.drawable.receiver_text_message_background);
                holder.receiverMessageText.setTextColor(Color.WHITE);
                holder.receiverMessageText.setGravity(Gravity.LEFT);
                holder.receiverMessageText.setText(messages.getMessage());
            }
        }

    }

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

/// my modal class

Messages.java

package com.MwandoJrTechnologies.the_smart_parent.Chats;


//modal class to retrieve messages
public class Messages {

    public String date, time, type, message, from;

    public Messages(){
        //empty default constructor
    }

    //generate constructor with parameters

    public Messages(String date, String time, String type, String message, String from) {
        this.date = date;
        this.time = time;
        this.type = type;
        this.message = message;
        this.from = from;
    }

    //generate getter and setter


    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }
}

上面的代码在FirebaseRecycler适配器中没有给我任何结果,但我希望它能在FirebaseRecycler适配器中显示

1 个答案:

答案 0 :(得分:1)

我认为答案在下面。

private void DisplayReceiverInformation() {
    receiverName.setText(messageReceiverName);
    rootReference.child("Users").child(messageReceiverID).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {

                            //retrieve from db
                            final String userName = dataSnapshot.child("fullName").getValue().toString();
                            final String profileImage = dataSnapshot.child("profileImage").getValue().toString();

                            //set values
                            receiverName.setText(userName);
                            Picasso.get().load(profileImage).placeholder(R.drawable.profile_image_placeholder).into(receiverProfileImage);

            messageAdapter = new MessagesAdapter( messageList);
            userMessagesList = (RecyclerView) findViewById(R.id.messages_list_of_users);
            linearLayoutManager = new LinearLayoutManager(this);
            userMessagesList.setHasFixedSize(true);
            userMessagesList.setLayoutManager(linearLayoutManager);
            userMessagesList.setAdapter(messageAdapter);
                     }
                }

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

                }
            });
        //here the data will be null so you must display data inside onDataChange() method.
}