3
在我的應用程序中,我有一個RecyclerView,它包含一個簡單的項目視圖,該視圖由每行中的TextView和一個微調器組成。如何迭代RecyclerView項目
用戶從操作欄單擊「保存」後,我需要迭代所有項目並獲取微調器選定值。
任何想法如何能夠或應該實現?
UPDATE:
我設法使用下面的循環迭代,但它是一個很好的解決方案調用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();
}
});
}
}
}
你想要的值點擊保存後,'sStatus'的狀態? – Moudiz
是的,但我需要所有RecyclerView項目的sStatus值。所以如果我有10行,我需要循環它們並獲得所有的sStatus的值 –
你可以發佈整個Recycler-View-Code嗎? – Mike76