1

I want my page to have two spinners and want to populate the second spinner on the basis of selection of the first spinner . I took two arrays . The first array is or the 1st spinner and the three arrays are for the second spinner . On the value selected from the first spinner I want the second spinner to select the corresponding values .

I have also referenced get selected value from second spinner on the basis of selected value of first spinner

in this post , the author has used three adapters for three arrays to populate the second spinner but I was thinking of using only two . Is it possible ?

Simple Android two spinner and submit example

This code isn't showing any errors but the app isn't opening .

 Spinner spin,spin1;


String state[] = new String[]{"A","B","C"};
String A[]= new String[]{"Pink","Blue","Red","white"};
String B[]= new String[]{"Apple","Grapes","Banana","Orange"};
String C[]= new String[]{"River","Mountains","Tree","Flower"};


ArrayAdapter<String> adap;
ArrayAdapter<String> adap1;


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

    spin = findViewById(R.id.spin);
    spin1 = findViewById(R.id.spin1);

    adap = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,state);


    spin.setAdapter(adap);


    spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            Toast.makeText(MainActivity.this, ""+ state[i], Toast.LENGTH_SHORT).show();

           if (state[i].equalsIgnoreCase("A")) {
               adap1.clear();
               adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, A);
               spin1.setAdapter(adap1);

           }
            if (state[i].equalsIgnoreCase("B")) {
                adap1.clear();
                adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,B);
                spin1.setAdapter(adap1);

            }

            if (state[i].equalsIgnoreCase("C")) {
                adap1.clear();
                adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, C);
                spin1.setAdapter(adap1);

            }

        }

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

        }

    });



    }
}
  • this can through null pointer excep bcz you have not initialized adap1 and on setOnItemSelectedListener adap1.clear(); will through null pointer.. remember when you set adapter as spin.setAdapter(adap);... setOnItemSelectedListener() called first time – Sanjay Kumar Jan 08 '18 at 12:47
  • How should I initialise the adap1 before the if condition as I am using adap1 to popularise the second spinner on the basis of the value chosen on the first spinner –  Jan 08 '18 at 12:54
  • just put adap1.clear(); out side the three block after toast message and check as if(adap1!=null){ adap1.clear()}.. note : in three block you are using same code adap1.clear(); so you can put it outside – Sanjay Kumar Jan 08 '18 at 12:57

2 Answers2

0

Remove adap1.clear(); from all if conditions

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
0

Check of null pointer exp -

spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(MainActivity.this, "" + state[i], Toast.LENGTH_SHORT).show();
                if (adap1 != null) {
                    adap1.clear();
                }


                if (state[i].equalsIgnoreCase("A")) {

                    adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, A);
                    spin1.setAdapter(adap1);

                }
                if (state[i].equalsIgnoreCase("B")) {

                    adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, B);
                    spin1.setAdapter(adap1);

                }

                if (state[i].equalsIgnoreCase("C")) {

                    adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, C);
                    spin1.setAdapter(adap1);

                }

            }

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

            }

        });
Sanjay Kumar
  • 1,135
  • 14
  • 27