我有两个微调器,第二个微调器的值取决于第一个微调器。微调框中的每个项目都有一个来自JSON
的ID。现在,我想获取第二个微调器的项目ID。我得到所选项目的名称,但我需要所选项目的名称和ID。
call.enqueue(new Callback<Map<String, ResponseJSON2>>()
{
@Override
public void onResponse
(Call<Map<String, ResponseJSON2>> call, retrofit2.Response<Map<String, ResponseJSON2>> response) {
responseJson = response.body();
if (responseJson != null) {
for (Map.Entry<String, ResponseJSON2> e : responseJson.entrySet()) {
provincesList.add(e.getKey());
for (Model c : e.getValue().getModel()) {
citiesList.add(c.getName());
}
}
}
createAdapter();
}
}
谢谢。
Model.java
public class Model {
private String id;
private String name;
private String taxonomy;
private String description;
private String images;
private String parent;
//getter and setters
@Override
public String toString() {
return "model{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", taxonomy='" + taxonomy + '\'' +
", description='" + description + '\'' +
", images='" + images + '\'' +
", parent='" + parent + '\'' +
'}';
}
}
答案 0 :(得分:1)
您还必须为城市创建Stream<Object>
:
Listener
然后使用此代码,您可以获得spinner_city.setOnItemSelectedListener(citiesListener);
cityId
答案 1 :(得分:0)
可以替换活动的完整代码
public class MainActivity extends AppCompatActivity {
private ApiInterface apiService;
private Spinner spinner_province;
private Spinner spinner_city;
private ArrayAdapter<String> provinceAdapter;
private ArrayAdapter<String> cityAdapter;
private ArrayList<String> provincesList = new ArrayList<>();
private ArrayList<String> citiesList = new ArrayList<>();
private ArrayList<String> citiesIdList = new ArrayList<>();
private Map<String, ResponseJSON> responseJson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
apiService = ApiClient.getClient().create(ApiInterface.class);
Call<Map<String, ResponseJSON>> call = apiService.getProvinceAndCity();
call.enqueue(new Callback<Map<String, ResponseJSON>>() {
@Override
public void onResponse(Call<Map<String, ResponseJSON>> call, Response<Map<String, ResponseJSON>> response) {
responseJson = response.body();
for (Map.Entry<String, ResponseJSON> e : responseJson.entrySet()) {
provincesList.add(e.getKey());
for (City c : e.getValue().getCity()) {
citiesList.add(c.getCityName());
}
}
createAdapter();
}
@Override
public void onFailure(Call<Map<String, ResponseJSON>> call, Throwable t) {
}
});
}
private void initUI() {
spinner_city = findViewById(R.id.spinner_cities);
spinner_province = findViewById(R.id.spinner_provinces);
}
private void createAdapter() {
provinceAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, provincesList);
provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_province.setAdapter(provinceAdapter);
cityAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, citiesList);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_city.setAdapter(cityAdapter);
spinner_province.setOnItemSelectedListener(provinceListener);
spinner_city.setOnItemSelectedListener(citiesListener);
}
private AdapterView.OnItemSelectedListener provinceListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
citiesList.clear();
citiesIdList.clear();
String provinceSelected = spinner_province.getItemAtPosition(position).toString();
for (Map.Entry<String, ResponseJSON> e : responseJson.entrySet()) {
if (e.getKey().equals(provinceSelected)) {
for (City c : e.getValue().getCity()) {
citiesList.add(c.getCityName());
citiesIdList.add(c.getCityId());
}
}
}
cityAdapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
private AdapterView.OnItemSelectedListener citiesListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int city_id = Integer.parseInt(citiesIdList.get(position));
Log.i("your_city_id", String.valueOf(city_id));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}