Spinner how to change the data
Mickey. The question was asked: Apr 15, 2020. 09:31
1 answer
I initially have 2 spinners in my project. I want to change the data of the 2nd Spinner when the first spinner values are changed. Here's what I have tried.
Spinner subscription = findViewById(R.id.spinner_subscription); items_subscription = new String[]{"Daily"}; ArrayAdapter<String> adapter_subscriptions = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items_subscription); subscription.setAdapter(adapter_subscriptions); payment_method.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if(position == 0){ items_subscription = new String[]{"Daily"}; }else if(position == 1){ items_subscription = new String[]{"Monthly", "Annually"}; }else if(position == 2){ items_subscription = new String[]{"Monthly", "Annually"}; }else{ items_subscription = new String[]{"Daily"}; } adapter_subscriptions.notifyDataSetChanged(); subscription.setAdapter(adapter_subscriptions); } @Override public void onNothingSelected(AdapterView<?> parent) { } });
And the above doesn't work. It only ads "Daily" on load, and when Spinner1 values change, the spinner2 values do not change. How can I achieve the following ?