在我的应用程序中,我有2个spinners
,并且这个spinners
应该从服务器填充。
Spinner
1显示国家名称,Spinner
2显示州。
我要选择spinner
个每个国家,spinner
2个显示该国家的州。
我的列表数据是:
{
"status": "success",
"data": [
{
"provinces": "USA",
"cites": [
"New York",
"Alabama",
"Alaska",
"Arizona",
"Arkansas",
"California",
"Colorado",
"Connecticut"
]
},
{
"provinces": "German"
"cites": [
"Bavaria",
"Baden-Württemberg",
"North Rhine-Westphalia",
"Hesse",
"Lower Saxony",
"Rhineland-Palatinate",
"Thuringia",
"Brandenburg"
]
}
]
}
我的代码:
public class AddressActivity extends AppCompatActivity {
@BindView(R.id.spinner)
Spinner countrySpinner;
@BindView(R.id.spinner2)
Spinner citiesSpinner;
private Context context;
private String apiHash;
private List<String> countriesNameList = new ArrayList<>();
private List<String> citiesNameList = new ArrayList<>();
private ArrayAdapter<String> dataAdapter;
private ArrayAdapter<String> dataAdapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address);
context = this;
ButterKnife.bind(this);
apiHash = GoodPrefs.getInstance().getString(API_HASH, "");
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesNameList);
callCitiesApi();
}
private void callCitiesApi() {
ApiClient.Companion.getInstance().apisUseCase()
.getCartCities(apiHash)
.enqueue(new Callback<CartCitiesResponse>() {
@Override
public void onResponse(Call<CartCitiesResponse> call, Response<CartCitiesResponse> response) {
if (response.isSuccessful()) {
if (Objects.requireNonNull(response.body()).getStatus().equals("success")) {
for (int i = 0; i < response.body().getData().size(); i++) {
countriesNameList.add(response.body().getData().get(i).getProvinces());
}
dataAdapter.notifyDataSetChanged();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countrySpinner.setAdapter(dataAdapter);
}
}
}
@Override
public void onFailure(Call<CartCitiesResponse> call, Throwable t) {
}
});
}
例如::spinner
1选择美国时,spinner
2显示美国的州,而{{ 1}} 1选择德国,spinner
2显示德国的状态和...
我可以用国家名称填写spinner
1,但是我不知道如何用spinner
1来更改spinner
2项!
我怎么办?
答案 0 :(得分:1)
使用onItemSelectedListener
并获取如下所示的城市:
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
when (parent?.id) {
R.id.provinceSpinner -> {
val provinceName = provinceSpinner?.getItemAtPosition(position)
// get cities here using provinceName
val cities = ...
// Fill with cities
cityAdapter?.clear()
cityAdapter?.addAll(cities)
cityAdapter?.notifyDataSetChanged()
}
}
}
建议:首先从服务器获取所有数据,然后将其本地存储在数据库中。然后在本地进行操作。
在此处完成实施
public class AddressActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
@BindView(R.id.spinner)
Spinner countrySpinner;
@BindView(R.id.spinner2)
Spinner citiesSpinner;
private Context context;
private String apiHash;
private List<String> countriesNameList = new ArrayList<>();
private List<String> citiesNameList = new ArrayList<>();
Map<String, List<String>> citiesNameMapWithCountry = new HashMap<>();
private ArrayAdapter<String> dataAdapter;
private ArrayAdapter<String> dataAdapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address);
context = this;
ButterKnife.bind(this);
apiHash = GoodPrefs.getInstance().getString(API_HASH, "");
//Attach adapter to spinner here
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesNameList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countrySpinner.setAdapter(dataAdapter);
//Add listener here
countrySpinner.setOnItemSelectedListener(this);
//Set city spinner with adapter
dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, citiesNameList);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
citiesSpinner.setAdapter(dataAdapter2);
callCitiesApi();
}
private void callCitiesApi() {
ApiClient.Companion.getInstance().apisUseCase()
.getCartCities(apiHash)
.enqueue(new Callback<CartCitiesResponse>() {
@Override
public void onResponse(Call<CartCitiesResponse> call, Response<CartCitiesResponse> response) {
if (response.isSuccessful()) {
if (Objects.requireNonNull(response.body()).getStatus().equals("success")) {
for (int i = 0; i < response.body().getData().size(); i++) {
String countryName = response.body().getData().get(i).getProvinces();
countriesNameList.add(countryName);
citiesNameMapWithCountry.put(countryName, response.body().getData().get(i).getCites());
}
//dataAdapter.clear();
//dataAdapter.addAll(countriesNameList);
dataAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onFailure(Call<CartCitiesResponse> call, Throwable t) {
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(parent.getId() == R.id.spinner) {
String countryName = (String) countrySpinner.getItemAtPosition(position);
citiesNameList.clear();
citiesNameList.addAll(citiesNameMapWithCountry.get(countryName));
//dataAdapter2.clear();
//dataAdapter2.addAll(citiesNameList);
dataAdapter2.notifyDataSetChanged();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}