Skip to content Skip to sidebar Skip to footer

How To Get A Text View Value From Selected Item In Recyclerview

I am trying to get a TextView value out of the selected item within the RecyclerView and send this value to an other activity . My RecyclerView layout fi

Solution 1:

In your itemView.setOnClickListener in the ViewHolder class , in the line intent.putExtra("key_prenom",nom);, replace variable nom with this:

 list.get(getAdapterPosition()).getNom();

Solution 2:

I have made changes in your class. Please have a look. Do implement listener in your activity

publicclassApiPersListAdapterextendsRecyclerView.Adapter<ApiPersListAdapter.ViewHolder> {
      publicinterfaceOnItemClickListener {
        voidonItemClick(ContentItem item);
    }
     privatefinal OnItemClickListener listener;
    TextView text1;
    Handlerhandler=newHandler();
String s;

    private Context context;
private List<Personne> list;
String nom;

publicApiPersListAdapter(Context context, List<Personne> list,OnItemClickListener listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
        }

@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Viewv= LayoutInflater.from(context).inflate(R.layout.recyclerview_api_list_item, parent, false);
        returnnewViewHolder(v);
        }

@OverridepublicvoidonBindViewHolder(ViewHolder holder, int position) {
 holder.bind(items.get(position), listener);




        }


@OverridepublicintgetItemCount() {
        return list.size();
        }

publicclassViewHolderextendsRecyclerView.ViewHolder {
    public TextView nom_pre,email;


    publicViewHolder(View itemView) {
        super(itemView);
        nom_pre = itemView.findViewById(R.id.nom_pren);
        email = itemView.findViewById(R.id.email);

        itemView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                listener.onItemClick(item);
            }
        });

Solution 3:

You can try this on your onCreateViewHolder

LayoutInflaterinflater= LayoutInflater.from(getApplicationContext());
Viewv= inflater.inflate(R.layout.recyclerview_api_list_item, null);

To get or set value to the textview of R.layout.recyclerview_api_list_item recycleview you need to find the view first so you can access them by the defined view.

v.findViewById(R.id.textviewID1);
v.findViewById(R.id.textviewID2);

Then return the view

returnnewViewHolder(v);

Post a Comment for "How To Get A Text View Value From Selected Item In Recyclerview"