英文:
I can't get the text from edittext in recycler view java by using recycler view adapter with .get(position)
问题
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<item_type> mImageNames;
private Context mContext;
public RecyclerViewAdapter(List<item_type> mImageNames, Context mContext) {
this.mImageNames = mImageNames;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
item_type type = mImageNames.get(position);
if ("MSG_TYPE_GROUP".equals(type.getType())) {
holder.imageNames.setText(type.getName());
holder.last_messages.setText("that trick won");
}
if ("MSG_TYPE_PERSONAL".equals(type.getType())) {
holder.imageName.setText(type.getName());
holder.last_message.setText("that was a nice trick");
}
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, ChatActivity.class);
intent.putExtra("usernames", mImageNames.get(position));
mContext.startActivity(intent);
}
});
holder.grouplayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, chat_group.class);
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mImageNames.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView image, imagess;
TextView imageName, last_message, imageNames, last_messages;
RelativeLayout parentLayout, grouplayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.images);
imageName = itemView.findViewById(R.id.imageName);
last_message = itemView.findViewById(R.id.last_mess);
parentLayout = itemView.findViewById(R.id.parent_layout);
imagess = itemView.findViewById(R.id.imagess);
imageNames = itemView.findViewById(R.id.imageNames);
last_messages = itemView.findViewById(R.id.last_messs);
grouplayout = itemView.findViewById(R.id.group);
}
}
}
英文:
I am new to java programming. I am trying to create a chatting application and now i want to get the the text in a specific RecyclerView
layout EditText
. I have used the below code as the recycler view adapter code. But it is showing error on getting the mImagenames.get(position)
in line 60. I need mImageNames.get(position)
work correctly. Any help is deeply appreciated. Thanks in Advance
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>
{
private List<item_type> mImageNames;
private Context mContext;
public RecyclerViewAdapter(List<item_type> mImageNames, Context mContext) {
this.mImageNames = mImageNames;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item,parent,false);
ViewHolder holder =new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
item_type type =mImageNames.get(position);
if("MSG_TYPE_GROUP".equals(type.getType())){
holder.imageNames.setText(type.getName());
holder.last_messages.setText("that trick won");
}
if("MSG_TYPE_PERSONAL".equals(type.getType())){
holder.imageName.setText(type.getName());
holder.last_message.setText("that was a nice trick");
}
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(mContext,ChatActivity.class);
intent.putExtra("usernames",mImageNames.get(position));
mContext.startActivity(intent);
}
});
holder.grouplayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(mContext,chat_group.class);
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mImageNames.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
CircleImageView image,imagess;
TextView imageName,last_message,imageNames,last_messages;
RelativeLayout parentLayout,grouplayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.images);
imageName=itemView.findViewById(R.id.imageName);
last_message=itemView.findViewById(R.id.last_mess);
parentLayout=itemView.findViewById(R.id.parent_layout);
imagess=itemView.findViewById(R.id.imagess);
imageNames=itemView.findViewById(R.id.imageNames);
last_messages=itemView.findViewById(R.id.last_messs);
grouplayout=itemView.findViewById(R.id.group);
}
}
}
答案1
得分: 2
经过查看您的代码,很明显您正在尝试传递带有意图的对象
intent.putExtra("usernames", mImageNames.get(position).getName());
如果您想要传递名称,请尝试上述代码。如果您想要将对象传递到下一个活动,您可以使该类实现可包裹接口(Parcelable)。
欲了解更多详情,请参阅此链接。
英文:
After looking at your code it is clear that you are trying to pass object with intent
intent.putExtra("usernames",mImageNames.get(position).getName());
try this if you want to pass the name. and if you want to pass the object to next activity you can make that class parcelable .
Check out this https://stackoverflow.com/a/7181792/3995126 for more details
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论