In my app i have a RecyclerView with a simple item view that consists of a TextView and a Spinner in each row.
After the user clicks "Save" from the action bar, i need to iterate over all the items and get the Spinner selected value.
Any ideas how this can or should be achieved?
UPDATE:
I have managed to iterate using the following loop, but is it a good solution calling findViewById() for every row since it's a heavy function to call performance wise?
for (int i = 0; i < rvAttendanceItems.getChildCount(); i++) {
Spinner spinner = (Spinner) rvAttendanceItems.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.attendance_list_status);
spinner.getSelectedItemId();
}
My full RecyclerView code:
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();
}
});
}
}
}
You could add an ArrayList<Spinner> to your AttendanceAdapter and add the Spinners to the ArrayList in the constructor of AttendanceViewHolder . Then you can iterate over all spinners after a button click. An other way would be the getChildAt() method of recyclerview.
You already made the call to findViewById()
in the view holder and don't need to make it again:
for (int i = 0; i < rvAttendanceItems.getChildCount(); i++) {
AttendanceViewHolder holder = (AttendanceViewHolder) rv.findViewHolderForAdapterPosition(i);
holder.sStatus.getSelectedItemId();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With