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

Asked

Viewed 880 times

2

I tried using the following code but 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);
        }`

1 answer

1

Just use the concept of parentesco:

The first item of the first spinner is selected, then the data of the second spinner 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

            }
        });

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.