我有一个recyclerview,其中显示了学生详细信息列表。我面临的问题是,当我单击第一项时,它将返回第二项的数据。
我相信它必须与getAdapterPosition()
做些事情,但是我不确定该怎么做。
在这种情况下,当选择第一个学生时,它将返回第二个学生(项目)的卷号。感谢您的帮助
RecyclerViewAdapter
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
Context context;
public List<StudentDataModel> dataModels;
public StudentAdapter(List<StudentDataModel> getDataAdapter, Context context){
super();
this.dataModels = getDataAdapter;
this.context = context;
}
public StudentDataModel dataAdapter;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {
dataAdapter = dataModels.get(position);
viewHolder.NameStudentCard.setText(dataAdapter.getName());
viewHolder.RankStudentCard.setText(dataAdapter.getRank());
viewHolder.RollStudentCard.setText(dataAdapter.getRoll());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notifyDataSetChanged();
Toast.makeText(v.getContext(), "Tapped on" + dataAdapter.getRoll(), Toast.LENGTH_SHORT).show(); //Here it returns the Roll No. of the second student(item) in the recyclerView.
Intent intent = new Intent (v.getContext(), StudentDetailsActivity.class);
v.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return dataModels.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView NameStudentCard;
public TextView RankStudentCard;
public TextView RollStudentCard;
public ViewHolder(View itemView) {
super(itemView);
NameStudentCard=(TextView)itemView.findViewById(R.id.textViewName);
RankStudentCard=(TextView)itemView.findViewById(R.id.textViewRank);
RollStudentCard =(TextView)itemView.findViewById(R.id.textViewEmployeeID);
}
}
}
答案 0 :(得分:3)
是的,因为您将对象声明为全局对象。因此它正在与最后一个可见行的对象进行更新,因为onBindViewHolder()
被依次为每个可见行调用。因此,它仅适用于最后一个可见行。
所以您需要在方法内部声明它:
对此评论:
// public StudentDataModel dataAdapter;
在这里声明:
@Override
public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {
final StudentDataModel dataAdapter = dataModels.get(position); // add this here
}
答案 1 :(得分:1)
尝试此代码。 使接口进入适配器类,以进行这种单击处理。.
onItemClickListner onItemClickListner;
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner{
void onItemClick(String data); // pass any data;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
// below code handle click event on recycler view item.
String data=mStringList.get(position);
holder.textView.setText(data);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onItemClick(data); //pass your data;
}
});
}
在将适配器绑定到recyclerview之后,适配器不为null,然后在下面的代码中调用。
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
@Override
public void onItemClick(String data) {
// perform your operation.
recyclerViewAdpater.notifyDataSetChanged();
}
});
答案 2 :(得分:0)
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
Context context;
public List<StudentDataModel> dataModels;
public StudentAdapter(List<StudentDataModel> getDataAdapter, Context context){
super();
this.dataModels = getDataAdapter;
this.context = context;
}
public StudentDataModel dataAdapter;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final StudentAdapter.ViewHolder viewHolder, final int position) {
dataAdapter = dataModels.get(position);
viewHolder.NameStudentCard.setText(dataAdapter.getName());
viewHolder.RankStudentCard.setText(dataAdapter.getRank());
viewHolder.RollStudentCard.setText(dataAdapter.getRoll());
}
@Override
public int getItemCount() {
return dataModels.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView NameStudentCard;
public TextView RankStudentCard;
public TextView RollStudentCard;
public ViewHolder(View itemView) {
super(itemView);
NameStudentCard=(TextView)itemView.findViewById(R.id.textViewName);
RankStudentCard=(TextView)itemView.findViewById(R.id.textViewRank);
RollStudentCard =(TextView)itemView.findViewById(R.id.textViewEmployeeID);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Tapped on" + dataModels.get(getAdapterPosition()).getRoll(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent (v.getContext(), StudentDetailsActivity.class);
v.getContext().startActivity(intent);
}
});
}
}
}