Spinner ItemSelected覆盖彼此

时间:2015-05-02 08:33:07

标签: java android spinner android-spinner

我的代码中有15个微调器,它们应该从我已创建的数组中获取值。代码如下;

private double[] grade_values = {-100000.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0};

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ece_second_year_second_semester);

        spinner_ids = new int[15];
        spinner_check = 0;
        spinner_index = 0;

        String[] grades = getResources().getStringArray(R.array.grades);

        spinner0 = (Spinner) findViewById(R.id.spinner0);
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner3 = (Spinner) findViewById(R.id.spinner3);
        spinner4 = (Spinner) findViewById(R.id.spinner4);
        spinner5 = (Spinner) findViewById(R.id.spinner5);
        spinner6 = (Spinner) findViewById(R.id.spinner6);
        spinner7 = (Spinner) findViewById(R.id.spinner7);
        spinner8 = (Spinner) findViewById(R.id.spinner8);
        spinner9 = (Spinner) findViewById(R.id.spinner9);

        extraSpinner0 = (Spinner) findViewById(R.id.extra_spinner0);
        extraSpinner1 = (Spinner) findViewById(R.id.extra_spinner1);
        extraSpinner2 = (Spinner) findViewById(R.id.extra_spinner2);
        extraSpinner3 = (Spinner) findViewById(R.id.extra_spinner3);
        extraSpinner4 = (Spinner) findViewById(R.id.extra_spinner4);

        spinner0.setOnItemSelectedListener(this);
        spinner1.setOnItemSelectedListener(this);
        spinner2.setOnItemSelectedListener(this);
        spinner3.setOnItemSelectedListener(this);
        spinner4.setOnItemSelectedListener(this);
        spinner5.setOnItemSelectedListener(this);
        spinner6.setOnItemSelectedListener(this);
        spinner7.setOnItemSelectedListener(this);
        spinner8.setOnItemSelectedListener(this);
        spinner9.setOnItemSelectedListener(this);

        extraSpinner0.setOnItemSelectedListener(this);
        extraSpinner1.setOnItemSelectedListener(this);
        extraSpinner2.setOnItemSelectedListener(this);
        extraSpinner3.setOnItemSelectedListener(this);
        extraSpinner4.setOnItemSelectedListener(this);

        each_grade = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, grades);

        each_grade.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner0.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner1.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner2.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner3.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner4.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner5.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner6.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner7.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner8.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));
        spinner9.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected,this));

        extraSpinner0.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected, this));
        extraSpinner1.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected, this));
        extraSpinner2.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected, this));
        extraSpinner3.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected, this));
        extraSpinner4.setAdapter(new NothingSelectedSpinnerAdapter(each_grade, R.layout.contact_spinner_row_nothing_selected, this));

    }

以“额外”命名的旋转器将其可见性设置为GONE但是我有一个按钮,使每个都可以看到。即按一次额外出现,按两次,另一次加入第一次,依此类推,直到五次完成。

onItemSelected方法如下;

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        int b = parent.getId();
        if (spinner_index < 15) {
            spinner_ids[spinner_index] = b;
            spinner_index++;

        }    
        setSpinner(b, position);
    }

    public void setSpinner(int spin_id, int value) {

        if (spin_id == spinner_ids[0])
            gpa_grade_0 = grade_values[value];
        else if (spin_id == spinner_ids[1])
            gpa_grade_1 = grade_values[value];
        else if (spin_id == spinner_ids[2])
            gpa_grade_2 = grade_values[value];
        else if (spin_id == spinner_ids[3])
            gpa_grade_3 = grade_values[value];
        else if (spin_id == spinner_ids[4])
            gpa_grade_4 = grade_values[value];
        else if (spin_id == spinner_ids[5])
            gpa_grade_5 = grade_values[value];
        else if (spin_id == spinner_ids[6])
            gpa_grade_6 = grade_values[value];
        else if (spin_id == spinner_ids[7])
            gpa_grade_7 = grade_values[value];
        else if (spin_id == spinner_ids[8])
            gpa_grade_8 = grade_values[value];
        else if (spin_id == spinner_ids[9])
            gpa_grade_9 = grade_values[value];
        else if (spin_id >= spinner_ids[10])
            new_gpa_grade_0 = grade_values[value];
        else if (spin_id >= spinner_ids[11])
            new_gpa_grade_1 = grade_values[value];
        else if (spin_id >= spinner_ids[12])
            new_gpa_grade_2 = grade_values[value];
        else if (spin_id >= spinner_ids[13])
            new_gpa_grade_3 = grade_values[value];
        else
            new_gpa_grade_4 = grade_values[value];

    }

现在的问题是,当我添加一个extraSpinner时,它正确地收到了值。但是当我添加另一个时,前一个不再起作用,因为这个新值取值。 gpa_grade_9之前的'else if'语句工作正常,但对于new_gpa_grades,添加的最后一个使得之前添加的语句停止工作并给出错误的答案。

有关如何设置onItemSelectedListener或修复我的代码的任何更好的想法将不胜感激。感谢。

如果您不理解我的问题,请告诉我,以便我可以改写。

0 个答案:

没有答案