Reputation: 732
I've a spinners in the Activity
the Spinners has 2 values
The Default Value for the Spinner Is: Attending
the User Will Change some Students Attend State to Absent.
The Problem is when the Spinners Count Exceed 10 Rows and user scroll the page to see remaining rows.
the upper Spinners value return do Default value which is Attening
i try to put a function to save the Value of the selected value from the spinner but i think there is another way to do it.
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.List;
public class AttendantsAdapter extends RecyclerView.Adapter<AttendantsAdapter.AttendantsHolder> {
private List<Student> students;
private int[] studentsAttendance;
public AttendantsAdapter(List<Student> items) {
this.students = items;
studentsAttendance = new int[students.size()];
}
@Override
public AttendantsHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attendant, parent, false);
return new AttendantsHolder(view);
}
@Override
public void onBindViewHolder(final AttendantsHolder holder, final int position) {
final Student student = students.get(position);
holder.nameText.setText(student.getName());
holder.attendingSpinner.setSelection(student.getAttendanceStatus());
holder.attendingSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(final AdapterView<?> parent, final View view, final int selected, final long id) {
studentsAttendance[position] = selected;
}
@Override
public void onNothingSelected(final AdapterView<?> parent) {
}
});
}
@Override
public int getItemCount() {
return students.size();
}
@Override
public long getItemId(final int position) {
return students.get(position).getId();
}
public List<Student> getStudents() {
return students;
}
public int[] getStudentsAttendance() {
return studentsAttendance;
}
public static class AttendantsHolder extends RecyclerView.ViewHolder {
public TextView nameText;
public Spinner attendingSpinner;
public AttendantsHolder(final View itemView) {
super(itemView);
nameText = (TextView) itemView.findViewById(R.id.studentName);
attendingSpinner = (Spinner) itemView.findViewById(R.id.spinner);
}
}
}
Upvotes: 3
Views: 3118
Reputation: 5045
By Creating LinkedHashMap which will contains key as a student_id and its value as a attendance type which will be used in a spinner. Below things you need implement at the time of creating Student Model class iteration.
// iterate listitem & fill LinkedHashMap where use student_id(Student.getId()) as key
// and set studentattendance position of spinner set as a value
LinkedHashMap<String, String> mapAttendanceData=new LinkedHashMap<String,String>();
//loop list<Student> & fill mapValue
mapAttendanceData.put("SET_STUDENT_ID_VALUE_FROM_RESPONSE_LIST","SET_SPINNER_SELECTED_POSITION_HERE");
How to Call
adapter = new AttendantsAdapter(ring.getStudents(),mapAttendanceData);
Adapter Class
public class AttendantsAdapter extends RecyclerView.Adapter<AttendantsAdapter.AttendantsHolder> {
private List<Student> students;
private int[] studentsAttendance;
// Here you can also use sparse array too;
private LinkedHashMap<String, String> mapAttendanceData;
public AttendantsAdapter(List<Student> items, LinkedHashMap<String, String> mapAttendanceData) {
this.students = items;
this.mapAttendanceData = mapAttendanceData;
studentsAttendance = new int[students.size()];
}
@Override
public AttendantsHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attendant, parent, false);
return new AttendantsHolder(view);
}
@Override
public void onBindViewHolder(final AttendantsHolder holder, final int position) {
final Student student = students.get(position);
holder.nameText.setText(student.getName());
// at very first time before changing from spinner. it will display data which you have set from iteration.
// and if its updated from spinner onItemSelected than it will display updated
holder.attendingSpinner.setSelection(Integer.parseInt(mapAttendanceData.get(student.getId())));
holder.attendingSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(final AdapterView<?> parent, final View view, final int selected, final long id) {
studentsAttendance[position] = selected;
// Update your mapvalue student id wise and selected
mapAttendanceData.put(student.getId(), selected + "");
}
@Override
public void onNothingSelected(final AdapterView<?> parent) {
}
});
}
@Override
public int getItemCount() {
return students.size();
}
@Override
public long getItemId(final int position) {
return students.get(position).getId();
}
public List<Student> getStudents() {
return students;
}
public int[] getStudentsAttendance() {
return studentsAttendance;
}
public static class AttendantsHolder extends RecyclerView.ViewHolder {
public TextView nameText;
public Spinner attendingSpinner;
public AttendantsHolder(final View itemView) {
super(itemView);
nameText = (TextView) itemView.findViewById(R.id.studentName);
attendingSpinner = (Spinner) itemView.findViewById(R.id.spinner);
}
}
}
Please check the comment in Adapter class & let me know if anything.
Upvotes: 3
Reputation: 1601
You have to maintain the Position value (Absent/Attending) while using the RecyclerView.Set the tag to the spinner
holder.attendingSpinner.setTag(students.get(position));
Upvotes: 1