Java 从自定义ListView元素内的微调器获取SelectEdItem

Java 从自定义ListView元素内的微调器获取SelectEdItem,java,android,listview,spinner,Java,Android,Listview,Spinner,我在谷歌上搜索了很多,但没有结果。我有一个自定义的ListView,每个元素都有几个文本视图和3个微调器和按钮。当我在微调器上设置值并按下按钮时,我想从这些微调器中获取所选值并将其发送到服务器。从顶部到最后一个可见的元素,列表元素的一切都很好,但对于不可见或在一半时间内不可见的项目,则会发生两件糟糕的事情 如果我更改微调器上的值,按按钮保存,则会发生两种情况,具体取决于我使用的图元: 如果元素有一半,或者在activity load上有一点可见,并且我对它执行了这些步骤,那么传递的值就是我更改它

我在谷歌上搜索了很多,但没有结果。我有一个自定义的ListView,每个元素都有几个文本视图和3个微调器和按钮。当我在微调器上设置值并按下按钮时,我想从这些微调器中获取所选值并将其发送到服务器。从顶部到最后一个可见的元素,列表元素的一切都很好,但对于不可见或在一半时间内不可见的项目,则会发生两件糟糕的事情

如果我更改微调器上的值,按按钮保存,则会发生两种情况,具体取决于我使用的图元:

  • 如果元素有一半,或者在activity load上有一点可见,并且我对它执行了这些步骤,那么传递的值就是我更改它之前设置的值

  • 如果元素根本不可见,并且我在上面执行了一些步骤,那么应用程序就会崩溃,NullPointerException将错误链接到微调器的声明

  • 代码:

    适配器:

    public View getView(final int position, View convertView, final ViewGroup parent) {
    
                StudentList students = studentList.get(position);
    
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
                final View view = inflater.inflate(R.layout.student_list, null);
    
                TextView id = (TextView) view.findViewById(R.id.list_id);
                TextView name = (TextView) view.findViewById(R.id.list_name);
                TextView surname = (TextView) view.findViewById(R.id.list_surname);
                Spinner t1 = (Spinner) view.findViewById(R.id.list_t1);
                Spinner t2 = (Spinner) view.findViewById(R.id.list_t2);
                Spinner t3 = (Spinner) view.findViewById(R.id.list_t3);
                TextView final = (TextView) view.findViewById(R.id.list_final);
                Button save = (Button) view.findViewById(R.id.save);
    
                id.setText(students.getId());
                name.setText(students.getName());
                surname.setText(students.getSurname());
                final.setText(students.getFinal());
    
                for (int i=0;i<t1.getCount();i++){
                    if (t1.getItemAtPosition(i).toString().equalsIgnoreCase(students.getT1())){
                        t1.setSelection(i);
                    }
                }
                for (int i=0;i<t2.getCount();i++) {
                    if (t2.getItemAtPosition(i).toString().equalsIgnoreCase(students.getT2())) {
                        t2.setSelection(i);
                    }
                }
    
                for (int i=0;i<t3.getCount();i++){
                    if (t3.getItemAtPosition(i).toString().equalsIgnoreCase(students.getT3())){
                        t3.setSelection(i);
                    }
                }
    
                if(t1.getSelectedItemPosition() > 1){
                    t2.setEnabled(false);
                    t3.setEnabled(false);
                } else if (t2.getSelectedItemPosition() > 1){
                    t3.setEnabled(false);
                }
    
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
                        ((ListView) parent).performItemClick(v, position, 0);
                    }
                });        
    
    
    
                if(studentList.get(position).getProtokol().equals("open")){
    
                } else {
                    t1.setEnabled(false);
                    t2.setEnabled(false);
                    t3.setEnabled(false);
                    save.setEnabled(false);
                }
    
    
                return view;
            }
    
    public View getView(最终int位置、视图转换视图、最终视图组父视图){
    StudentList students=StudentList.get(position);
    LayoutFlater充气器=(LayoutFlater)上下文.getSystemService(LAYOUT\u充气器\u服务);
    最终视图=充气机。充气(R.layout.student_列表,空);
    TextView id=(TextView)view.findViewById(R.id.list\u id);
    TextView name=(TextView)view.findViewById(R.id.list\u name);
    TextView姓氏=(TextView)view.findViewById(R.id.list\u姓氏);
    微调器t1=(微调器)view.findViewById(R.id.list_t1);
    微调器t2=(微调器)视图.findViewById(R.id.list_t2);
    微调器t3=(微调器)view.findViewById(R.id.list\u t3);
    TextView final=(TextView)view.findViewById(R.id.list\u final);
    按钮保存=(按钮)视图.findViewById(R.id.save);
    id.setText(students.getId());
    name.setText(students.getName());
    name.setText(students.getname());
    final.setText(students.getFinal());
    
    对于(int i=0;i我在谷歌上搜索了2天,最后决定在这里发布,但1小时后我找到了解决方案。更改了所有微调器,如下所示:

    Spinner t1 = (Spinner) parent.getChildAt(position-parent.parent.getFirstVisiblePosition()).findViewById(R.id.lista_termin1);
    
    Spinner t1 = (Spinner) parent.getChildAt(position-parent.parent.getFirstVisiblePosition()).findViewById(R.id.lista_termin1);