I can't get the text from edittext in recycler view java by using recycler view adapter with .get(position)

huangapple go评论77阅读模式
英文:

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&lt;RecyclerViewAdapter.ViewHolder&gt;
{
private List&lt;item_type&gt; mImageNames;
private Context mContext;
public RecyclerViewAdapter(List&lt;item_type&gt; 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(&quot;MSG_TYPE_GROUP&quot;.equals(type.getType())){
holder.imageNames.setText(type.getName());
holder.last_messages.setText(&quot;that trick won&quot;);
}
if(&quot;MSG_TYPE_PERSONAL&quot;.equals(type.getType())){
holder.imageName.setText(type.getName());
holder.last_message.setText(&quot;that was a nice trick&quot;);
}
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(mContext,ChatActivity.class);
intent.putExtra(&quot;usernames&quot;,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(&quot;usernames&quot;,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

huangapple
  • 本文由 发表于 2020年9月1日 17:54:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63685390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定