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

Tags: , ,



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);
        }
    }
}

Answer

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



Source: stackoverflow