Search code examples
javaandroidandroid-layoutandroid-activity

Android: Is there a way to switch the spinner array based on user input from other activity?


I am a beginner in Android development. I am trying to make an app for a class project. During registration (Registration activity) the user can choose any continent from the spinner. In another activity, the user gets to choose countries depending on what continent the user had chosen before from the spinner on registration activity. I already have different arrays in my string.xml file.

My thought process is, in the second activity, if the user chose Asia, this layout will have the spinner display countries in Asia. If the user chose Europe, the spinner will display countries in Europe (array from strings.xml) example of my array on string.xml

 <string-array name="asia">
    <item>"India"</item>
    <item>"pakistan"</item>
    <item>"China"</item>
    <item>"Japan"</item>
    <item>"Bangladesh"</item>
</string-array>

I also have a string array for Europe and other continents. I have a spinner on my layout, but I don't know how to implement the different arrays based on user input from first activity. I haven't assigned any arrays/options on my spinner yet. Any kind of help/suggestions/resource/advice is highly appreciated.


Solution

  • You could either have a method in your second activity which updates the spinners adapter with a string array.

    Or you could have a public variable as an array which acts as your selected array. This array could be updated based on the selected state of the spinner. Then you can access this array from your second activity and use it when you set the adapter of your spinner.

    The last one of the two is probably the easiest.

    Here is an example of how you could do it. First activity contains a spinner and a button which takes you to the second

    First activity:

    //The spinner in the first activity
    Spinner spinner;
    
    //the lists that we use in our spinner
    public List<String> asiaList = new ArrayList<String>();
    public List<String> europeList = new ArrayList<String>();
    
    //Our selected array
    public List<String> selectedArray = new ArrayList<String>();
    
    
    public static MainActivity mainActivity;
    
    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mainActivity = this;
    
    
        //Getting the xml arrays into our lists
        getListInfo(getResources().getStringArray(R.array.asia), asiaList);
        getListInfo(getResources().getStringArray(R.array.europe), europeList);
    
    
    
        //Your spinner
        spinner = findViewById(R.id.spinner1);
    
        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();
    
        //Items in your first spinner;
        categories.add("Asia");
        categories.add("Europe");
    
        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
    
        // Drop down layout style
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    
    
        //Isn't necessary and can be done by using spinner.getSelectedItemPosition()
        // Spinner click listener
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
                //If asia is selected
                if(position == 0){
                    selectedArray = asiaList;
                }else{
                    selectedArray = europeList;
                }
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });
    
    
    }
    //function for your button to open next activity
    public void OpenSecondActivity(View v){
            Intent myIntent = new Intent(MainActivity.this, secondActivity.class);
            startActivity(myIntent);
    secondActivity.selectedArray = selectedArray;
    
    
            
    
    }
    
    
    //Put xml strings into your array lists;
    public void getListInfo(String[] strings, List<String> list){
        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }
    
    
    }
    

    Second Activity:

    //Spinner second class
    Spinner spinner;
    
    public static List<String> selectedArray = new ArrayList<String>();
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    
    
        spinner = findViewById(R.id.spinner2);
    
    
        // Creating adapter for spinner and also reference the current list from first activity to get right info
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, selectedArray);
    
        // Drop down layout style
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    
    }
    

    The strings.xml:

    <string-array name="asia">
        <item>"India"</item>
        <item>"pakistan"</item>
        <item>"China"</item>
        <item>"Japan"</item>
        <item>"Bangladesh"</item>
    </string-array>
    
    <string-array name="europe">
        <item>"Sweden"</item>
        <item>"England"</item>
        <item>"Spain"</item>
        <item>"Norway"</item>
        <item>"Germany"</item>
    </string-array>