• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I cannot get the chats to display in the recycler adapter, I have already in firebase database and as well have attached firebase recycler adapter

enter image description here

I have tried some of the ways of getting the recycler adapter to display the chats but unable to display my adapter

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

The above code does not give me any result in the FirebaseRecycler adapter yet I expect it to display in the FirebaseRecycler adapter

I think the answer is below.

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.
}
  • 1
Reply Report
      • 1
    • I am still new to this, please help me understand more,how do I implement that? Because I think I can display inside onDataChange but cannot display chats
      • 1
    • I am using firebase i have note that if we set the adapter outside the onDataChange() then it will ne null. So i we must have to put set adapter inside the onDataChange (). You can call your adapter inside onDataChange() .
    • For fetch messages from firebase database you have to get reference, you done it by using usersDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(fromUserID); but for messages you are not getting reference.
      • 2
    • the users database reference is only used to fetch the users details only but for the messages I have used a different reference which is the root i.e. rootReference.child("Messages").child(messageSenderID).child(messageReceiverID)