Aditya Raj
Aditya Raj

Reputation: 31

How to load an array/list of string values in Spinner excluding one value?

I need to create two Spinners (both displaying a list of locations) with the following rules:

Let's say I have a universal set of locations as under:

Consider that the locations in these 4 lists lie along 4 unique routes as listed above.

Now, I want to populate all the locations in the universal set in Spinner-1 (i.e. countries on all the 4 routes), which will serve as a "From" location selector.

Once a location is selected from Spinner-1, I want to populate Spinner-2 (which serves as a "To" location selector) only with those locations which appear on the specific route on which the selected location lies. Also, I want to exclude the selected location from Spinner-1 from displaying in Spinner-2.

For eg., if Mexico is selected from Spinner-1, Spinner-2 should display USA, Canada, Jamaica...

What is the best mechanism to program this requirement?

Upvotes: 2

Views: 3967

Answers (2)

Arjun saini
Arjun saini

Reputation: 4182

Try this

String[] regionsArray;
List<String> regions;
Spinner spinner;

regionsArray=getResources().getStringArray(R.array.Country_array);
regions=new ArrayList<String>();
regions = new ArrayList<String>(Arrays.asList(regionsArray));

/* selected item will look like a spinner set from XML */
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, regions);

spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);


/* Your ARRAY Defined in xml file */
<string-array name="Country_array">
  <item>India</item>
  <item>Japan</item>
  <item>China</item>
</string-array>

Upvotes: 0

Shree Krishna
Shree Krishna

Reputation: 8562

OK I have done it now, The Main Logic is

  1. Create an array and adapter for first spinner.
  2. Inside the onItemSelectedListener method of first spinner, remove the selected element and set adapter to second spinner.

Let's have a look

You have an ArrayList of countries like this

final ArrayList<String> countries = new ArrayList<>();
    countries.add("India");
    countries.add("Japan");
    countries.add("China");
    countries.add("South Korea");
    countries.add("Nepal");

And you have two spinner (spinner & spinner2)

Spinner spinner1 = (Spinner) findViewById(R.id.mySpinner1);
final Spinner spinner2 = (Spinner) findViewById(R.id.mySpinner2); //It is final because it was accessed within inner class but if you don't want so then declare it as class level variable

You made an Adapter for spinner first

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, countries);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(spinnerArrayAdapter);

And inside onItemSelected of spinner remove the selected element from array and set adapter to second spinner that is spinner2

spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                 ArrayList<String> tempCounties = (ArrayList<String>) countries.clone();

               tempCounties.remove(position);
               ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, tempCounties);
               spinnerArrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
               spinner2.setAdapter(spinnerArrayAdapter2);
            }

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

            }
        });

Upvotes: 1

Related Questions