spinner selection defines choices for another spin

2019-06-10 20:43发布

问题:

I have a question about spinners, and cannot find anything online about how to do it.

I have a spinner in my app, with region selections i.e. Southwest, Southeast.

I want the spinner to selections available of the second spinner to be dependent on the selection made in the first one. i.e. When the user selects Southwest the choices on the second spinner would be Lake Charles, Iowa, Lake Arthur etc. And when the user selects southeast, the choices of the second spinner would be Gretna, New Orleans, Luling etc.

how do I go about doing that?

examples would be greatly appreciated!

Thank you!

回答1:

Try this Code..

I hope it will helpful to you...

public class MainActivity extends Activity {

Spinner sp1,sp2;
ArrayAdapter<String> adp1,adp2;
List<String> l1,l2;
int pos;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    l1=new ArrayList<String>();

    l1.add("A");
    l1.add("B");

    sp1= (Spinner) findViewById(R.id.spinner1);
    sp2= (Spinner) findViewById(R.id.spinner2);

    adp1=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,l1);
    adp1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    sp1.setAdapter(adp1);

    sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            pos=arg2;
            add();

        }

        private void add() {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show();

            switch(pos)
            {
            case 0:
                l2= new ArrayList<String>();                    
                l2.add("A 1");
                l2.add("A 2");

                adp2=new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_dropdown_item_1line,l2);
                adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                sp2.setAdapter(adp2);

                select();

                break;
            case 1:
                l2= new ArrayList<String>();                    
                l2.add("B 1");
                l2.add("B 2");

                adp2=new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_dropdown_item_1line,l2);
                adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                sp2.setAdapter(adp2);

                select();

                break;
            }

        }

        private void select() {
            // TODO Auto-generated method stub

            sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getBaseContext(), "Test "+arg2, Toast.LENGTH_SHORT).show();

                }

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

                }
            });

        }

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

        }
    });
    }

}


回答2:

Well essentially you would catch the first spinners selection event. Upon this you would change the contents of the list availabe for the second spinner. You might also have to call invalidateView() in order for the change to take effect. So there are really three parts

1) Catch the selection in a onClick handler.

2) modify the data for the second spinner, and reset spinner2 data to the adjusted data after filtering.

3) Invalidate the second spinner if needed, and possibly invalidateView on the containingView so it all shows up.

So break it down to these three steps and let us know if you have questions about any of them.