微调值不更新
Spinner value not update
这是我的 Country Spinner,在这里我获取国家 ID 并使用国家代码传入 ApiGetState 我正在获取州列表,在获取州列表后我想在 State Spinner 中设置它,我已经完成了所有操作但是如果我 select 国家第一次工作正常,但如果我改变国家,状态微调器不更新,我是否遗漏了这段代码中的某些内容?
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
scountry = parent.getItemAtPosition(position).toString();
String cCode = scountry.substring(0, scountry.indexOf("-"));
countryCode = Integer.parseInt(cCode);
ApiGetStates(countryCode);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我在这里从服务器设置 StateSpinner 的值:-
public void onResponse(Call<StatesModel> call, Response<StatesModel> response) {
if (response.isSuccessful()){
StatesModel model = response.body();
List<StatesModel.StatesModelDetail> list = model.getData();
for (int i = 0; i < list.size(); i++){
stateData = list.get(i);
String stateName = stateData.getName();
String code = stateData.getId();
String finalname = code + "-" + stateName;
arrayList.add(finalname);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.simple_spinner_dropdown, arrayList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
我做了这件事。只需清除“ApiGetStates(countryCode);”之前的数组列表并调用“adapter.notifyDataSetChanged();”根据 Sandeep Pareek,在“for 循环”之外。例如:-
arrayList.clear();
ApiGetStates(countryCode);
这是我的 Country Spinner,在这里我获取国家 ID 并使用国家代码传入 ApiGetState 我正在获取州列表,在获取州列表后我想在 State Spinner 中设置它,我已经完成了所有操作但是如果我 select 国家第一次工作正常,但如果我改变国家,状态微调器不更新,我是否遗漏了这段代码中的某些内容?
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
scountry = parent.getItemAtPosition(position).toString();
String cCode = scountry.substring(0, scountry.indexOf("-"));
countryCode = Integer.parseInt(cCode);
ApiGetStates(countryCode);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我在这里从服务器设置 StateSpinner 的值:-
public void onResponse(Call<StatesModel> call, Response<StatesModel> response) {
if (response.isSuccessful()){
StatesModel model = response.body();
List<StatesModel.StatesModelDetail> list = model.getData();
for (int i = 0; i < list.size(); i++){
stateData = list.get(i);
String stateName = stateData.getName();
String code = stateData.getId();
String finalname = code + "-" + stateName;
arrayList.add(finalname);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.simple_spinner_dropdown, arrayList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
我做了这件事。只需清除“ApiGetStates(countryCode);”之前的数组列表并调用“adapter.notifyDataSetChanged();”根据 Sandeep Pareek,在“for 循环”之外。例如:-
arrayList.clear();
ApiGetStates(countryCode);