dabreo
dabreo

Reputation: 53

How do I access the item in FirebaseRecycler's getItemViewType()?

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() {

            ....
    });
}

Upvotes: 1

Views: 200

Answers (3)

dabreo
dabreo

Reputation: 53

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

Upvotes: 0

Gobu CSG
Gobu CSG

Reputation: 691

Let try this,

  if (mFirebaseUser.getUid() == options.get(position). getUid()) 

Upvotes: 2

Kuba Spatny
Kuba Spatny

Reputation: 26978

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

Upvotes: 1

Related Questions