1

I am building a chat functionality within an app. The messages sent by user will be on the right side and messages received by the user on left side of the screen. All the chat data is stored in a pojo. In FirebaseRecycler#getItemViewType() method I want to compare the UID of the current item/pojo with mFirebaseUser.getUid() and assign the layout accordingly.

How do I access the item within the getItemViewType()?

pojo message

public class message {

private String id;
private String text;
private String name;
private String photoUrl;
private String imageUrl;
private String uid;

public message() {
}

public message(String text, String name, String photoUrl, String imageUrl, String uid) {
    this.text = text;
    this.name = name;
    this.photoUrl = photoUrl;
    this.imageUrl = imageUrl;
    this.uid = uid;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public void setText(String text) {
    this.text = text;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhotoUrl() {
    return photoUrl;
}

public String getText() {
    return text;
}

public void setPhotoUrl(String photoUrl) {
    this.photoUrl = photoUrl;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getUid() {

    return uid;
}
}

FirebaseRecyler Method.

private void getMessages() {

    final int VIEW_TYPE_MESSAGE_SENT = 1;
    final int VIEW_TYPE_MESSAGE_RECEIVED = 2;

    FirebaseRecyclerOptions<message> options =
            new FirebaseRecyclerOptions.Builder<message>()
                    .setQuery(messagesRef, parser)
                    .build();
    mFirebaseAdapter = new FirebaseRecyclerAdapter<message, RecyclerView.ViewHolder>(options) {

        @Override
        public int getItemViewType(int position) {
            if (mFirebaseUser.getUid() == ?) {
                // If the current user is the sender of the message
                // Based on the UID associated with the message and current UID
                return VIEW_TYPE_MESSAGE_SENT;

                } else {
                // If some other user sent the message
                return VIEW_TYPE_MESSAGE_RECEIVED;
            }
        }

        @Override
        @NonNull
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

            if(viewType == VIEW_TYPE_MESSAGE_SENT) {

                LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
                return new SentMessageViewHolder(inflater.inflate(R.layout.sent_item_message, viewGroup, false));

            } else if(viewType == VIEW_TYPE_MESSAGE_RECEIVED) {

                LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
                return new MessageViewHolder(inflater.inflate(R.layout.item_message, viewGroup, false));
            }

            return null;
        }

        @Override
        protected void onBindViewHolder(RecyclerView.ViewHolder viewHolder,
                                        int position,
                                        @NonNull message friendlyMessage) {
            mProgressBar.setVisibility(ProgressBar.INVISIBLE);
            switch (viewHolder.getItemViewType()) {
                case VIEW_TYPE_MESSAGE_RECEIVED:
                    ((MessageViewHolder) viewHolder).bind(friendlyMessage);
                    break;
                case VIEW_TYPE_MESSAGE_SENT:
                    ((SentMessageViewHolder) viewHolder).bind(friendlyMessage);
                    break;
            }

        }
    };

    mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

            ....
    });
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
dabreo
  • 53
  • 1
  • 9

3 Answers3

2

Let try this,

  if (mFirebaseUser.getUid() == options.get(position). getUid()) 
Gobu CSG
  • 653
  • 5
  • 7
  • This works till the fragment is active but after navigating to other activity and coming back to chat fragment, all the message gets displayed on the left side. – dabreo Jun 29 '18 at 12:44
  • if you navigate time your mFirebaseUser.getUid() log that... u will fix that – Gobu CSG Jun 29 '18 at 12:50
1

The question is how to access the item within getViewItemType(). For that use the getItem(position) method.

    @Override
    public int getItemViewType(int position) {
          if (mFirebaseUser.getUid() == getItem(position).getUid()) {
               // If the current user is the sender of the message
               // Based on the UID associated with the message and current UID
               return VIEW_TYPE_MESSAGE_SENT;

          } else {
               // If some other user sent the message
               return VIEW_TYPE_MESSAGE_RECEIVED;
          }
    }
Kuba Spatny
  • 26,618
  • 9
  • 40
  • 63
  • Well then try debugging it to see where is the problem. Either `mFirebaseUser.getUid()` is wrong or `getItem(position).getUid()`. – Kuba Spatny Jun 29 '18 at 12:48
  • The problem was with condition in if statement TextUtils.equals() fixed the issue. – dabreo Jun 30 '18 at 11:14
0

The solution was quite simple as pointed out by other answers. The only problem with the other two answers were condition checking in if statement, which showed the sent message on right only till the page got refreshed. The solution was to use equals() method from TextUtils class.

@Override
        public int getItemViewType(int position) {
            if (TextUtils.equals(mFirebaseUser.getUid(), getItem(position).getUid())) {
                // If the current user is the sender of the message
                return VIEW_TYPE_MESSAGE_SENT;

                } else {
                // If some other user sent the message
                return VIEW_TYPE_MESSAGE_RECEIVED;
            }
        }
dabreo
  • 53
  • 1
  • 9