城市列表不显示基于android中州名称的选择



Hi在下面的代码中选择状态名称。城市名称列表正在填充。但在选择州名称后,城市名称只显示城市名称的最后一个元素。城市名称包含一个列表,但列表不按州名称显示。根据我的邮递员回复,显示了州名,但没有显示城市名。

有人能帮我哪里做错了吗。

if (response.isSuccessful()) {
Log.e("response", new Gson().toJson(response.body()));
GetStateMasterDataModel getStateMasterDataModel = response.body();
final List<StateMstrModel> getStateMasterDataModels = getStateMasterDataModel.getStateMstr();
for (StateMstrModel stateMstrModel : getStateMasterDataModels) {
StateNames = stateMstrModel.getState();
CityNames = stateMstrModel.getCity();
DistrictNames = stateMstrModel.getDistric();
StateMstrModel stateMstrModel1 = new StateMstrModel(StateNames, CityNames, DistrictNames);
stateDataList.add(stateMstrModel1);
//  ArrayList<String> values=new ArrayList<String>();
hashSet = new HashSet<String>();
hashSet.addAll(stateNames);
stateNames.clear();
stateNames.addAll(hashSet);
stateNames.add(StateNames);
Collections.sort(stateNames, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
CityList.add(CityNames);
districtNames.add(DistrictNames);
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinner_item, stateNames);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(spinnerArrayAdapter);
spinnerArrayAdapter.notifyDataSetChanged();
state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position+1>0){
String sp1= String.valueOf(state.getSelectedItem());
if(stateNames.contains(sp1)){
ArrayList<String> CityList=new ArrayList<>();
CityList.add(CityNames);
final ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(
getApplicationContext(),R.layout.spinner_item,CityList);
spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerArrayAdapter1.notifyDataSetChanged();
city.setAdapter(spinnerArrayAdapter1);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

邮差回复:

{
"Message": "State Master List",
"Status": true,
"StateMstr": [
{
"Id": "1",
"State": "Maharashtra",
"Distric": "Mumbai City",
"City": "Mumbai"
}, 
{
"Id": "8",
"State": "Maharashtra",
"Distric": "Pune",
"City": "Pune"
},
{
"Id": "13",
"State": "Maharashtra",
"Distric": "Nagpur",
"City": "Nagpur"
}
}]}

只显示城市名称的最后一个元素

因为您正在传递城市ArrayAdapter中最后存储的城市名称变量。所以你需要通过城市名称列表

为每个循环生成一个数组列表以超出ur

ArrayList<String> CityList=new ArrayList<>();

然后在每个循环的ur内的列表中添加您的城市名称

CityNames = stateMstrModel.getCity();
CityList.add(CityNames);

相关内容

  • 没有找到相关文章

最新更新