Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To convert the UserViewHolder to BotViewHolder without causing a java.lang.ClassCastException error, the following steps can be taken:

  1. Create a new layout file for the BotViewHolder with a different layout design.
  2. Create a new class named BotViewHolder that extends RecyclerView.ViewHolder and implements the necessary methods.
  3. Inside the BotViewHolder class, declare and initialize all views in the new layout file.
  4. Update the adapter to check the type of the ViewHolder being created and return the corresponding ViewHolder instance.
  5. In the onBindViewHolder method of the adapter, cast the ViewHolder based on its type and set the necessary data.

Example code:

  1. New layout file for BotViewHolder (botitemlayout.xml):
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/bot_avatar"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/bot_avatar"/>

    <TextView
        android:id="@+id/bot_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="16sp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_gravity="center_vertical"/>

</LinearLayout>
  1. New BotViewHolder class:
public class BotViewHolder extends RecyclerView.ViewHolder {

    private ImageView botAvatar;
    private TextView botMessage;

    public BotViewHolder(@NonNull View itemView) {
        super(itemView);
        botAvatar = itemView.findViewById(R.id.bot_avatar);
        botMessage = itemView.findViewById(R.id.bot_message);
    }

    public void bindBotMessage(String message) {
        botMessage.setText(message);
    }
}
  1. Updated adapter code:
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_USER = 1;
    private static final int VIEW_TYPE_BOT = 2;

    private List<ChatMessage> chatMessages;

    public ChatAdapter(List<ChatMessage> chatMessages) {
        this.chatMessages = chatMessages;
    }

    @Override
    public int getItemViewType(int position) {
        ChatMessage chatMessage = chatMessages.get(position);
        return chatMessage.isSentByUser() ? VIEW_TYPE_USER : VIEW_TYPE_BOT;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case VIEW_TYPE_USER:
                View userView = inflater.inflate(R.layout.user_item_layout, parent, false);
                return new UserViewHolder(userView);
            case VIEW_TYPE_BOT:
                View botView = inflater.inflate(R.layout.bot_item_layout, parent, false);
                return new BotViewHolder(botView);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ChatMessage chatMessage = chatMessages.get(position);
        if (holder instanceof UserViewHolder) {
            UserViewHolder userViewHolder = (UserViewHolder) holder;
            userViewHolder.bindUserMessage(chatMessage.getMessage());
        } else if (holder instanceof BotViewHolder) {
            BotViewHolder botViewHolder = (BotViewHolder) holder;
            botViewHolder.bindBotMessage(chatMessage.getMessage());
        }
    }

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