To convert the UserViewHolder to BotViewHolder without causing a java.lang.ClassCastException error, the following steps can be taken:
Example code:
<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>
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);
}
}
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();
}
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-06 17:06:46 +0000
Seen: 11 times
Last updated: May 06 '23
What is the usage of JavaScript in IntelliJ IDEA Community Edition?
What is the issue with using java Gson().fromJson to assign BigDecimal values?
How can Django Admin accommodate a variety of formats and locales for its input fields?
How can an array be passed using typo3 flexform xml and itemsProcConfig?