How to change the content of a spinner by making the selection in another spinner?

2

I tried to use the following code but the error

form.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch(form.getSelectedItemPosition()){
                case 0:{
                    i=R.array.tecnologicas;

                    break;
                }
                case 1:{
                    i=R.array.direito;

                    break;
                }
                case 2:{
                    i=R.array.administracao;

                    break;
                }
            }
            adapter_curso = ArrayAdapter.createFromResource(this,i,android.R.layout.simple_spinner_dropdown_item );
            adapter_curso.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            curso.setAdapter(adapter_curso);
        }'
    
asked by anonymous 18.03.2015 / 20:36

1 answer

1

Just use the concept of parentesco :

The first item of the primerpinner is selected, then the data of the secondpinner is exchanged. It occurs the same way when the second item is selected.

ArrayAdapter<String> segundospinnerAdapter;
ArrayAdapter<String> primeirospinnerAdapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_spinner_item,
                primeirospinnervalue);
        primeiro_spinner.setAdapter(primeirospinnerAdapter);
        primeirospinnerAdapter 
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


primeirospinner.setOnItemSelectedListener(new OnItemSelectedListener() {

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

                    // get data from database add to arraylist

                      segundospinnerAdapter = new
                      ArrayAdapter<String>( mainActivity.this,
                      android.R.layout.simple_spinner_item,   arraylistvalue);
                      segundo_spinner.setAdapter(segundospinnerAdapter);
                      segundo_spinner.setSelection(arg2, false);
                      segundospinnerAdapter.setDropDownViewResource
                      (android.R.layout.simple_spinner_dropdown_item);

            }

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

            }
        });

a>

    
18.03.2015 / 21:47