2

I have two spinners, when I select first spinner, based on first spinner selection, second spinner arrays to get change.How to do that.

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

In this two spinners, in first spinner, i have used "type", based on the selection of type of first spinner, how to change second spinner to use the array color and weeks?

Thanks in advance

4

3 回答 3

5

在第一个微调器中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
            }

        });
于 2013-06-10T04:57:33.027 回答
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。

于 2013-06-10T04:59:54.707 回答
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) {

            }
        });

    }

}
于 2020-06-15T04:13:36.043 回答