为什么我无法在MessageViewHolder类中声明变量?我想为我的xml文件找到viewById。顺便说一句,我正在最新的64位Ubuntu中运行最新版本的Android Studio。但是,肯定没有版本问题。我尝试了其他一些变量。同样的问题!
@NonNull
@Override
public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.message_single_layout, parent, false);
return new MessageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
Messages messages = messagesList.get(position);
holder.chatText.setText(messages.getMessage());
}
@Override
public int getItemCount() {
return messagesList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView chatText;
public CircleImageView chatImage;
public MessageViewHolder(View itemView) {
super(itemView);
}
chatText =
chatImage
}
}
答案 0 :(得分:1)
在视图持有人中,您从视图中获取ID
就这样-
public MessageViewHolder(View itemView) {
super(itemView);
chatText = itemView.findViewById(R.id.*);
chatImage = itemView.findViewById(R.id.*);
}
答案 1 :(得分:1)
您需要在 findViewById
内进行 MessageViewHolder()
,使用itemView
尝试一下
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView chatText;
public CircleImageView chatImage;
public MessageViewHolder(View itemView) {
super(itemView);
chatText =itemView.findViewById(R.id.chatText);
chatImage=itemView.findViewById(R.id.chatImage);
}
}