找到你要的答案

Q:Spinner ItemSelected overwriting eachother

Q:微调ItemSelected覆盖彼此

I have 15 spinners in my code and they are supposed to take values from an array I already created. code is given below;

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

    }

The spinners named with "extra" have their visibilities set to GONE but i have a button that makes each become visible at a time. i.e press once one extra appears, press twice, another joins the first extra and so on till the five are complete.

The onItemSelected method is as below;

 @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];

    }

The problem now is that when i add an extraSpinner, it recieves the value correctly. But when i add another, the previous one does not work again as this new one takes the value. The 'else if' statements up to gpa_grade_9 are working perfectly, but as for the new_gpa_grades, the last one added makes the ones added before to stop working and give wrong answer.

Any better ideas on how to set onItemSelectedListener or a fix to my code will be appreciated. Thanks.

Please if you don't understand my question, just let me know so i can rephrase.

我在我的代码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));

    }

该公司名为“额外”的可见性设置了但我有一个按钮,使得每一次变得可见。即按一次额外出现,按两次,另一个加入第一个额外的,直到五完成。

的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是完美的工作,但对于new_gpa_grades,最后一个加入使得新增之前停止工作并给出错误的答案。

任何好的想法如何设置onitemselectedlistener或修复我的代码会感激。谢谢.

如果你不明白我的问题,只是让我知道这样我就可以改变。

java  android  spinner  android-spinner