简体   繁体   中英

How do I use if-else in two Adapters

I have two adapters, one is for the country and another is continents.

What I want to happen is: When I pick a continent in the Continent's Spinner for example "Asia", the other countries which are: "Belgium", "France", "Italy", "Germany", "Spain" will not display in the list when I click the Countries' Spinner . Thus, only the "China" will display in the list.

Note: I don't know if the codes are working right I just based the continents code from a github post and edited it.

The link where I copied the codes: https://stackoverflow.com/questions/ask

Countries Adapter and Spinner:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         BetterSpinner textView = (BetterSpinner)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain", "China"
     };

Continents Adapter and Spinner:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, CONTINENTS);
         BetterSpinner textView = (BetterSpinner)
                 findViewById(R.id.continents_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Asia", "Europe", "North America", "South America"
     };

You can try this

Continents Adapter and Spinner:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                     android.R.layout.simple_dropdown_item_1line, CONTINENTS);
             BetterSpinner textView = (BetterSpinner)
                     findViewById(R.id.continents_list);
             textView.setAdapter(adapter);
             textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
                    String selectedContinent=CONTINENTS[pos];
                    if(selectedContinente.equals("Asia"){
                        //Code to generate another spinner for countries
                        setCountrySpinner();    

                    }

                }

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

                }
            });

         }

         private static final String[] CONTINENTS = new String[] {
             "Asia", "Europe", "North America", "South America"
         };

 private static final String[] ASIA_COUNTRIES = new String[] {
          "India","China"
     };

 private static final String[] OTHER_COUNTRIES = new String[] {
          "Belgium", "France", "Italy", "Germany", "Spain"
     };



private void setCountrySpinner(){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, ASIA_COUNTRIES);
         BetterSpinner textView = (BetterSpinner)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }
}

Using this way you need to make diffrent array for diffrent country. This is not the best way. i recommend you to make one Model class for this like:

private class CountryModel{
      private String continentName;
      private String countryName;

}

Using this model class you can fetch country name according to ContinentName.

For Countries instead of String array:

private static final String[] COUNTRIES = new String[] {
     "Belgium", "France", "Italy", "Germany", "Spain", "China"
 };

Use ArrayList of String arrays:

private static ArrayList COUNTRIES = new ArrayList<>();
arrayList.add(new String[]{"Belgium", "France", "Italy", "Germany", "Spain"});
arrayList.add(new String[]{"China"});

Now according to the user's selection position on the spinner you can simply show the countries from String Array of that position in ArrayList.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM