在我的应用程序中,我有一个带有简单项目视图的RecyclerView,每个行包含一个TextView和一个微调器。
用户点击"保存"从操作栏中,我需要迭代所有项目并获得Spinner选择的值。
有关这方面可以或应该如何实现的任何想法?
更新
我已经设法使用以下循环进行迭代,但它是一个很好的解决方案,为每一行调用findViewById(),因为它是一个很重要的函数来调用性能吗?
for (int i = 0; i < rvAttendanceItems.getChildCount(); i++) {
Spinner spinner = (Spinner) rvAttendanceItems.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.attendance_list_status);
spinner.getSelectedItemId();
}
我的完整RecyclerView代码:
public class AttendanceAdapter extends RecyclerView.Adapter<AttendanceAdapter.AttendanceViewHolder> {
private List<Student> studentItems;
private Fragment frag;
ArrayList<Status> statuses = new ArrayList<>();
public AttendanceAdapter(List<Student> studentItems, Fragment frag) {
this.studentItems = studentItems;
this.frag = frag;
DbHelper db = DbHelper.getInstance(frag.getContext());
Cursor c = db.getStatuses("attendance");
if (c != null) {
while (c.moveToNext()) {
Status tempStatus = new Status(c.getInt(c.getColumnIndex(DbHelper.STATUS_ID)), c.getInt(c.getColumnIndex(DbHelper.STATUS_COLOR)), c.getString(c.getColumnIndex(DbHelper.STATUS_DESC)), c.getString(c.getColumnIndex(DbHelper.STATUS_CATEGORY)));
statuses.add(tempStatus);
}
c.close();
} else {
Toast.makeText(frag.getContext(), "Could not retrieve status list", Toast.LENGTH_SHORT).show();
}
}
@Override
public AttendanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attendance, parent, false);
return new AttendanceViewHolder(itemView);
}
@Override
public void onBindViewHolder(final AttendanceViewHolder holder, int position) {
final Student current = studentItems.get(position);
SpinnerAdapter adapter = new SpinnerAdapter(frag.getContext(), R.layout.item_spinner_status, statuses);
holder.sStatus.setAdapter(adapter);
holder.tvName.setText(current.getFullName());
}
@Override
public int getItemCount() {
return studentItems.size();
}
public class AttendanceViewHolder extends RecyclerView.ViewHolder {
private final TextView tvName;
private final Spinner sStatus;
public AttendanceViewHolder(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.attendance_list_name);
sStatus = (Spinner) itemView.findViewById(R.id.attendance_list_status);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sStatus.performClick();
}
});
}
}
}
答案 0 :(得分:2)
您已在视图持有者中调用了public class <T> Me extends Thing<String, T, String>
...
// init with
Me<String> myMe = new Me<>();
,无需再次进行此操作:
findViewById()