I am getting a list that contains two lists of Time zones, of US and Canada from server. Id of Canada list is 1 and Us is 2.That lists contains string id and some other strings. I need that string id to be listed in spinner so that's why I am making new list that contains only strings that I need. I put id's of countries in a switch
statement and it should put list in spinner
and it works fine. But it shows only the first two items from list.
for (int i = 0; i < list.size(); i++)
listCountryId.add(list.get(0).getTimeZoneModelList().get(i).getId());
so basically idea is that I loop through my list from server and because I always have only two lists in that list, in this case I loop through first list. That is why I hardcoded. When I debug I can see that my getTimeZoneModelList()
contains full list but when I debug listCountryId()
it contains only first two items.
this is my full code:
private void setUpSpinner() {
switch (model.getCountry().getCoutryId()) {
case 1:
for (int i = 0; i < list.size(); i++)
listCountryId.add(list.get(0).getTimeZoneModelList().get(i).getId());
ArrayAdapter<String> adapterMembers = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, listCountryId);
adapterMembers.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
binding.timezoneSpinner.setAdapter(adapterMembers);
Log.i(TAG, "lista idjeva: " + Arrays.toString(listCountryId.toArray()));
break;
case 2:
for (int i = 0; i < list.size(); i++) {
listCountryId.add(list.get(1).getTimeZoneModelList().get(i).getId());
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(
context, android.R.layout.simple_spinner_item, listCountryId);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
binding.timezoneSpinner.setAdapter(adapter);
Log.i(TAG, "lista idjeva: " + Arrays.toString(listCountryId.toArray()));
break;
}
}
You are looping in the list
which contains only 2 (US & Canda), but actually you need the underlying list of codes
So, In case 1
: You need to change the list in the for loop from
for (int i = 0; i < list.size(); i++)
To:
for (int i = 0; i < list.get(0).getTimeZoneModelList().size(); i++)
And do the same for case 2
It is happenning because your loop is indeed running two times. It should be
for (int i = 0; i < list.get(0).getTimeZoneModelList().size(); i++)
listCountryId.add(list.get(0).getTimeZoneModelList().get(i).getId());
Even better:
List<TimeZoneModel> timeZoneModelList = listCountryId.add(list.get(0).getTimeZoneModelList();
for (int i = 0; i < timeZoneModelList.size(); i++)
listCountryId.add(timeZoneModelList.get(i).getId());
The second way is more efficient.
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.