滚动后,微调器选择的值已更改

时间:2016-01-12 08:28:59

标签: android spinner

我在活动

中有一个微调器

Spinners有2个值

  1. 主治
  2. Abbsent
  3. 微调器的默认值是:出席

    用户将改变一些学生参加国家缺席。

    问题是当Spinners计数超过10行并且用户滚动页面以查看剩余的行。

    上部的Spinners值返回执行默认值,即Attening

    我尝试使用一个函数来保存微调器中所选值的值,但我认为还有另一种方法可以做到。

    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);
            }
        }
    }
    

2 个答案:

答案 0 :(得分:3)

创建LinkedHashMap,它将包含key作为student_id,其值作为将在微调器中使用的出勤类型。 下面是您在创建Student Model类迭代时需要实现的内容。

    // 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");

如何致电

adapter = new AttendantsAdapter(ring.getStudents(),mapAttendanceData);

适配器类

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);
            }
        }
    }

请查看Adapter class&amp;中的注释。如果有的话,请告诉我。

答案 1 :(得分:1)

使用RecyclerView时,您必须保持位置值(缺席/出席)。将标签添加到微调器

holder.attendingSpinner.setTag(students.get(position));