Categories: java, android

Spinner how to change the data

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(;     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 ?

All answers to this question, which has the identifier 61238965

The best answer:

This happens because you changed the reference to the array items_subscription of your adapter when you reassigned it in inside onItemSelected, now your adapter_subscriptions doesn't have the same object as the array and you're modifying a data that the adapter has no knowledge about it. It's better to have an array list, clear it, and fill it with new data when they're changed then call adapter.notifyDataSetChanged().

ArrayList<String> items_subscription = new ArrayList<>(); // add your objects to the list and initialize the adapter // ... public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {     items_subscription.clear();     if(position == 0){         items_subscription.add("Daily");     }     // ...etc      adapter_subscriptions.notifyDataSetChanged(); } 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data