如何从RECYCLERVIEW中的选定项目获取TEXT VIEW值 [英] How to get a TEXT VIEW value from selected item in RECYCLERVIEW
问题描述
我正在尝试从RecyclerView中的选定项目中获取TextView值,并将该值发送到其他活动.我的RecyclerView布局文件有两个TextView,一个用于名字和姓氏,另一个用于电子邮件.我发现recyclerview不支持LIST VIEW之类的某些功能,如果有人可以为我提供一些建议,这就是我的代码.
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 file has two TextViews, one for the firstname and the lastname and the other one for the email . i found that recyclerview doesnt support some functionality like LIST VIEW , this is my code if any one can suggest for me something please.
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.Spannable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import com.example.lab.Activities.MainActivity;
import com.example.lab.Activities.ModifyPers;
import com.example.lab.Activities.NewPersActivity;
import com.example.lab.Entities.Personne;
import java.util.List;
public class ApiPersListAdapter extends RecyclerView.Adapter<ApiPersListAdapter.ViewHolder> {
TextView text1;
Handler handler =new Handler();
String s;
private Context context;
private List<Personne> list;
String nom;
public ApiPersListAdapter(Context context, List<Personne> list) {
this.context = context;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.recyclerview_api_list_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Personne p = list.get(position);
holder.nom_pre.setText(p.getPrenom()+" "+p.getNom());
holder.email.setText(p.getEmail());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView nom_pre,email;
public ViewHolder(View itemView) {
super(itemView);
nom_pre = itemView.findViewById(R.id.nom_pren);
email = itemView.findViewById(R.id.email);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(itemView.getContext(), ModifyPers.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.v("nom",nom);
intent.putExtra("key_prenom",nom);
itemView.getContext().startActivity(intent);
}
});
}
}
}
推荐答案
在您的itemView.setOnClickListener
中
在ViewHolder
类的intent.putExtra("key_prenom",nom);
行中,将变量nom
替换为:
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();
这篇关于如何从RECYCLERVIEW中的选定项目获取TEXT VIEW值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!