我有两个spinner.one位于代表国家的工具栏上,另一个位于代表该区域的片段。我从服务器获取数据当我选择特定区域的国家时我想这样做显示在区域微调器中。
这是我的json数据
{
"Status": 1,
"StatusMessage": "Country and Country Area List",
"data": [
{
"CountryID": "1",
"CountryName": "India",
"CountryArea": [
{
"CountryID": "1",
"AreaID": "1",
"AreaName": "Kuwait City(Capital)"
},
{
"CountryID": "1",
"AreaID": "2",
"AreaName": " Hawally"
},
{
"CountryID": "1",
"AreaID": "3",
"AreaName": "Farwaniya"
},
{
"CountryID": "1",
"AreaID": "4",
"AreaName": " Ahmadi"
},
{
"CountryID": "1",
"AreaID": "5",
"AreaName": " Jahra"
}
]
},
{
"CountryID": "2",
"CountryName": "Dubai",
"CountryArea": [
{
"CountryID": "2",
"AreaID": "6",
"AreaName": " Jeddah"
},
{
"CountryID": "2",
"AreaID": "7",
"AreaName": " Riyadh"
},
{
"CountryID": "2",
"AreaID": "8",
"AreaName": " Khobar"
},
{
"CountryID": "2",
"AreaID": "9",
"AreaName": " Abha"
},
{
"CountryID": "2",
"AreaID": "10",
"AreaName": " Abqiq"
},
{
"CountryID": "2",
"AreaID": "11",
"AreaName": " Abu Arish"
},
{
"CountryID": "2",
"AreaID": "12",
"AreaName": " Al Ahsa"
},
{
"CountryID": "2",
"AreaID": "13",
"AreaName": " Al Baha"
},
{
"CountryID": "2",
"AreaID": "14",
"AreaName": " Al Dhahran"
},
{
"CountryID": "2",
"AreaID": "15",
"AreaName": " Al Jubail"
},
{
"CountryID": "2",
"AreaID": "16",
"AreaName": " Al Kharj"
},
{
"CountryID": "2",
"AreaID": "17",
"AreaName": " Al Madina El Monawara"
},
{
"CountryID": "2",
"AreaID": "18",
"AreaName": " Al Qatif"
},
{
"CountryID": "2",
"AreaID": "19",
"AreaName": " Al Taif"
},
{
"CountryID": "2",
"AreaID": "20",
"AreaName": " Almadnab"
},
{
"CountryID": "2",
"AreaID": "21",
"AreaName": " Bisha"
},
{
"CountryID": "2",
"AreaID": "22",
"AreaName": " Buraida"
},
{
"CountryID": "2",
"AreaID": "23",
"AreaName": " Dawadmy"
},
{
"CountryID": "2",
"AreaID": "24",
"AreaName": " Hafr Elbatin"
},
{
"CountryID": "2",
"AreaID": "25",
"AreaName": " Hail "
},
{
"CountryID": "2",
"AreaID": "26",
"AreaName": " Jazan"
},
{
"CountryID": "2",
"AreaID": "27",
"AreaName": " Mecca"
},
{
"CountryID": "2",
"AreaID": "28",
"AreaName": " Oniza"
},
{
"CountryID": "2",
"AreaID": "29",
"AreaName": " Qurayat"
},
{
"CountryID": "2",
"AreaID": "30",
"AreaName": " Zulfi"
},
{
"CountryID": "2",
"AreaID": "31",
"AreaName": "Dammam"
}
]
}
]
}
这是我的CountryAdpter,位于工具栏:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
this.menu = menu;
MenuItem menuItem = menu.findItem(R.id.menu_spinner).setVisible(true);
mCountrySpinner = (Spinner) MenuItemCompat.getActionView(menuItem);
countryAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, mCountryList);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mCountrySpinner.setAdapter(countryAdapter);
mCountrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
countryId = getCountryId.get(position);
Log.e("countryid", countryId);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
答案 0 :(得分:0)
我将概述为了以正确的方式构建数据所需要执行的一般步骤。
1st。您需要解析给定的JSON。我高度建议使用 GSON 等库。它易于使用,非常有用。要使用GSON传递它,您必须至少创建两个类 - 国家/地区和 CountryArea 。类Country应该有一个CountryArea对象列表。确保变量名为mach 完全来自JSON的相应键,或使用GSON的 SerializedName 属性。
第二。当您点击 onItemSelected 时,填充第二个Spinner的方式与填充第一个Spinner的方式相同,但这次使用相应的Country Areas。