0

I want to do a spinner of cities, and that spinner will determine what place (which will be also a spinner) inside the city I select.

For example, if I selected "New York" and the spinner which called place will display for instance "Postal" and "Best Buy", now when I choose another city it will display "Postal" and "Walmart".

How can I make a class for all the cities or is there another way to do that?

WebDevBooster
  • 14,674
  • 9
  • 66
  • 70
Sagy Cohen
  • 23
  • 6

1 Answers1

0

try this

public class MainActivity extends AppCompatActivity {

private ArrayAdapter<City> adapter1;
private ArrayAdapter<Institutes> adapter2;
private Spinner spin1;
private Spinner spin2;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
    adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);

    spin1 = (Spinner) findViewById(R.id.spin1);
    spin2 = (Spinner) findViewById(R.id.spin2);

    spin1.setAdapter(adapter1);
    spin2.setAdapter(adapter2);

    spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            City city = adapter1.getItem(position);
            adapter2.clear();
            adapter2.addAll(city.getInstitutesList());
            spin2.setSelection(0);
            adapter2.notifyDataSetChanged();

        }

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

        }
    });

    City city1 = new City("city1");
    city1.getInstitutesList().add(new Institutes("Institutes1"));
    city1.getInstitutesList().add(new Institutes("Institutes2"));
    city1.getInstitutesList().add(new Institutes("Institutes3"));

    City city2 = new City("city2");
    city2.getInstitutesList().add(new Institutes("Institutes4"));
    city2.getInstitutesList().add(new Institutes("Institutes5"));
    city2.getInstitutesList().add(new Institutes("Institutes6"));

    adapter1.addAll(city1, city2);
    spin1.setSelection(0);

}


public static class City {
    private List<Institutes> institutesList = new ArrayList<>();
    private String name;

    public City(String name) {
        this.name = name;
    }

    public List<Institutes> getInstitutesList() {
        return institutesList;
    }

    public void setInstitutesList(List<Institutes> institutesList) {
        this.institutesList = institutesList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

public static class Institutes {
    private String name;

    public Institutes(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}
}

Result

Mohamd Ali
  • 2,146
  • 4
  • 23
  • 30