如何在第一个微调器android中选择时更改第二个微调器数组

时间:2013-06-10 04:28:44

标签: android android-layout android-intent android-listview android-spinner

我有两个微调器,当我选择第一个微调器时,根据第一个微调器选择,第二个微调器阵列进行更改。如何做到这一点。

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string-array name="type">
        <item>Select Type</item>
        <item >Color</item>
        <item >Weeks</item>
      </string-array>

    <string-array name="color">
        <item>Blue</item>
        <item >Orange</item>
        <item >Red</item>
        <item >Green</item>
       </string-array>

     <string-array name="weeks">
        <item>Sunday</item>
        <item >Monday</item>
        <item >Tuesday</item>
        <item >Wednesday</item>
              </string-array>

</resources>

  <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Select Type:"/>

  <Spinner
  android:id="@+id/spinner"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:prompt="@string/spinner_title"
  android:drawSelectorOnTop = "true"/>


    <Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
          android:prompt="@string/spinner_title1"
    android:layout_height="wrap_content" />

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.type, android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

在这两个微调器中,在第一个微调器中,我使用了“类型”,根据第一个微调器的类型选择,如何更改第二个微调器以使用阵列颜色和周?

提前致谢

3 个答案:

答案 0 :(得分:4)

第一个微调器OnItemSelectedListener中的

在下面进行..

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parentView,
                    View selectedItemView, int position, long id) {
                // Object item = parentView.getItemAtPosition(position);

                // Depend on first spinner value set adapter to 2nd spinner
            if(pos == 1){
              spinner2.setAdapter(adapter2);
            }else{
              spinner2.setAdapter(adapter3);
            } 

            }

            public void onNothingSelected(AdapterView<?> arg0) {// do nothing
            }

        });

答案 1 :(得分:0)

您可以动态地在微调器中添加值,而不是使用xml文件。 你可以在这样的onItemSelected方法中添加spn2中的值。

private ArrayAdapter<String> spn1Adpt;
private ArrayAdapter<String> spn2Adpt;

private ArrayList<String> spn1List = new ArrayList<String>();
private ArrayList<String> spn2List = new ArrayList<String>();

spn1Adpt= new MySpinnAdapter(this, spn1List );
spn2Adpt= new MySpinnAdapter(this, spn2List );

spn1.setAdapter(spn1Adpt);
spn2.setAdapter(spn2Adpt);

spn1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> par, View v, int p,
                long arg3) {

            listD.clear();

                spn2List.add("val");
                spn2List.add("val");
                snn2Adpt.notifyDataSetChanged();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

这里我使用了MySpinnAdapter这是您可以根据需要制作的自定义适配器,或者可以直接使用ArrayAdapter。

答案 2 :(得分:0)

这是另一种简单的方法。

这里有三个微调器,每个微调器相互依赖:

public class MainActivity extends AppCompatActivity {

    Spinner citySpinner;
    Spinner townSpinner;
    Spinner streetSpinner;

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

        citySpinner = findViewById(R.id.citySpinner);
        townSpinner = findViewById(R.id.townSpinner);
        streetSpinner = findViewById(R.id.streetSpinner);

        final String city[] = {"--Select Your District--", "Kozhikkode City"};
        final String kozhikkodeCity[] = {"--Select Your Town--", "vadakara Town","Nadapuram Town"};
        final String vadakaraTown[] = {"--Select Your Street--", "Orkkateri Street","vellikulangara Street"};
        final String nadapuramTown[] = {"--Select Your Street--", "Edacheri Street","Purameri Street"};

        ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,city);
        citySpinner.setAdapter(cityAdapter);

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


                if (position == 1){
                    ArrayAdapter<String> townAdapter =  new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item,kozhikkodeCity);
                    townSpinner.setAdapter(townAdapter);

                    townSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            if (position == 1){
                                ArrayAdapter<String> streetAdapter =  new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item,vadakaraTown);
                                streetSpinner.setAdapter(streetAdapter);
                            }else if (position == 2){
                                ArrayAdapter<String> streetAdapter =  new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item,nadapuramTown);
                                streetSpinner.setAdapter(streetAdapter);
                            }
                        }

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

                        }
                    });
                }
            }

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

            }
        });

    }

}