选择第一个更改时,第二个微调器的位置设置为默认值

时间:2015-10-15 09:57:13

标签: java android spinner

我有两个旋转器,两个都有2个项目。当我在微调器中更改我的选择时,我想将秒选择更改为默认值。 我怎么能实现这一目标? 这是我试过的代码

    @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    x = s1.getSelectedItemPosition();
    y = s2.getSelectedItemPosition();
    switch (x){
        case(0):
            y=0;
            switch (y){
                case(0):b1.setVisibility(View.VISIBLE);
                    b2.setVisibility(View.INVISIBLE);
                    b3.setVisibility(View.INVISIBLE);
                    b4.setVisibility(View.INVISIBLE);
                    c1.setVisibility(View.INVISIBLE);
                    c2.setVisibility(View.INVISIBLE);
                    break;
                case(1):
                    b1.setVisibility(View.INVISIBLE);
                    b2.setVisibility(View.INVISIBLE);
                    b3.setVisibility(View.INVISIBLE);
                    b4.setVisibility(View.INVISIBLE);
                    c1.setVisibility(View.VISIBLE);
                    c2.setVisibility(View.INVISIBLE);
                    break;
            }
            break;
        case(1):
            s2.setSelection(0);
            switch (y){
                case(0):b2.setVisibility(View.VISIBLE);
                    b1.setVisibility(View.INVISIBLE);
                    b3.setVisibility(View.INVISIBLE);
                    b4.setVisibility(View.INVISIBLE);
                    c1.setVisibility(View.INVISIBLE);
                    c2.setVisibility(View.INVISIBLE);
                    break;
                case(1):
                    b1.setVisibility(View.INVISIBLE);
                    b2.setVisibility(View.INVISIBLE);
                    b3.setVisibility(View.INVISIBLE);
                    b4.setVisibility(View.INVISIBLE);
                    c2.setVisibility(View.VISIBLE);
                    c1.setVisibility(View.INVISIBLE);
                    break;
            }
    }

2 个答案:

答案 0 :(得分:0)

我希望这段代码可以帮助你。

ArrayAdapter<String> secondspinnerAdapter;
ArrayAdapter<String> firstspinnerAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, firstspinnervalue);

first_spinner.setAdapter(firstspinnerAdapter);      
firstspinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
firstspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {                       
                        // set data in arraylist.
                      secondspinnerAdapter = new ArrayAdapter<String>( mainActivity.this,  android.R.layout.simple_spinner_item,  arraylistvalue);
                      second_spinner.setAdapter(secondspinnerAdapter);
                      second_spinner.setSelection(arg2, false);
                      secondspinnerAdapter.setDropDownViewResource
                      (android.R.layout.simple_spinner_dropdown_item);

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

请参阅此链接:How to set selected item of Spinner by value, not by position?

答案 1 :(得分:0)

如果要将第二个微调器更改为默认值,可以使用spinnerObject.setSelection(DEFAULT_INDEX)设置第二个微调器。

spinnerObject.setSelection(0);