Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private void loadSpinners() {
dialog = ProgressDialog.show(AddVisitActivity.this, "", getResources().getString(R.string.loading), true);
WebService.getService().getLocations().enqueue(new Callback<LocationListResponse>() {
@Override
public void onResponse(Call<LocationListResponse> call, Response<LocationListResponse> response) {
countries = response.body().getCountry();
cities = response.body().getCity();
categories = response.body().getLandmarkCategory();
landmarks = response.body().getLandmark();
LocationListResponse.Country header = new LocationListResponse.Country();
header.setEnName(AddVisitActivity.this.getResources().getString(R.string.countryh));
countries.add(0, header);
spinnerCountry.setAdapter(new ArrayAdapter<>(AddVisitActivity.this, android.R.layout.simple_list_item_1, countries));
spinnerCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LocationListResponse.Country country = countries.get(position);
int cid = country.getOId();
List<LocationListResponse.City> newcities = getCitiesofCountry(cid, cities);
spinnerCities.setAdapter(new ArrayAdapter<>(AddVisitActivity.this, android.R.layout.simple_list_item_1, newcities));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
LocationListResponse.LandmarkCategory header2 = new LocationListResponse.LandmarkCategory();
header2.setEnName(AddVisitActivity.this.getResources().getString(R.string.categoryh));
categories.add(0, header2);
spinnerLandmarkCategory.setAdapter(new ArrayAdapter<>(AddVisitActivity.this, android.R.layout.simple_list_item_1, categories));
spinnerLandmarkCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LocationListResponse.LandmarkCategory landmarkCategory = categories.get(position);
int catid = landmarkCategory.getOId();
List<LocationListResponse.Landmark> newLandMarks = getLandMarksofCategory(catid, landmarks);
spinnerLandmark.setAdapter(new ArrayAdapter<>(AddVisitActivity.this, android.R.layout.simple_list_item_1, newLandMarks));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
dialog.dismiss();
}
@Override
public void onFailure(Call<LocationListResponse> call, Throwable t) {
dialog.dismiss();
}
});
}
private List<LocationListResponse.City> getCitiesofCountry(int countryID, List<LocationListResponse.City> sourcCities) {
List<LocationListResponse.City> citiesofc = new ArrayList<>();
for (int i = 0; i < sourcCities.size(); i++) {
LocationListResponse.City current = sourcCities.get(i);
if (current.getCountryId() == countryID) {
citiesofc.add(current);
}
}
LocationListResponse.City header = new LocationListResponse.City();
header.setEnName(AddVisitActivity.this.getResources().getString(R.string.city));
citiesofc.add(0, header);
return citiesofc;
}
private List<LocationListResponse.Landmark> getLandMarksofCategory(int categoryID, List<LocationListResponse.Landmark> sourcLandmarks) {
List<LocationListResponse.Landmark> landmarkofc = new ArrayList<>();
for (int i = 0; i < sourcLandmarks.size(); i++) {
LocationListResponse.Landmark current = sourcLandmarks.get(i);
if (current.getCategoryId() == categoryID) {
landmarkofc.add(current);
}
}
LocationListResponse.Landmark header = new LocationListResponse.Landmark();
header.setEnName(AddVisitActivity.this.getResources().getString(R.string.landmarkheader));
landmarkofc.add(0, header);
return landmarkofc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment