我检查了这个问题的相关问题,并尝试了一些建议但仍然似乎没有执行setOnItemSelectedListener
事件。这是我的活动代码:
private void init() {
pays = (Spinner) findViewById(R.id.pays_spinner);
populatePays();
}
List<String> list;
public void populatePays(){
list = new ArrayList<String>();
apiService = RestService.createService(SolarAPIService.class);
Call<ArrayList<String>> call = apiService.listPays();
call.enqueue(new Callback<ArrayList<String>>() {
@Override
public void onResponse(Call<ArrayList<String>> call, Response<ArrayList<String>> response) {
for(String pays: response.body()) {
list.add(pays);
}
}
@Override
public void onFailure(Call<ArrayList<String>> call, Throwable t) {
}
});
ArrayAdapter<String> dataAdapter= new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
pays.setAdapter(dataAdapter);
pays.setSelection(1);
pays.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
nompays=pays.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(),
"Selected Country : " + nompays, Toast.LENGTH_LONG).show();
populateVille(nompays);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
感谢您的帮助。 谢谢!
答案 0 :(得分:0)
而不是:
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
试;
pays.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
答案 1 :(得分:0)
在填充列表后设置微调器适配器。
private void init() {
pays = (Spinner) findViewById(R.id.pays_spinner);
populatePays();
}
List<String> list;
public void populatePays() {
list = new ArrayList<String>();
apiService = RestService.createService(SolarAPIService.class);
Call<ArrayList<String>> call = apiService.listPays();
call.enqueue(new Callback<ArrayList<String>>() {
@Override
public void onResponse(Call<ArrayList<String>> call, Response<ArrayList<String>> response) {
for (String pays : response.body()) {
list.add(pays);
}
setSpinnerAdapter();
}
@Override
public void onFailure(Call<ArrayList<String>> call, Throwable t) {
}
});
private void setSpinnerAdapter() {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
pays.setAdapter(dataAdapter);
pays.setSelection(1);
pays.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
nompays = pays.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(),
"Selected Country : " + nompays, Toast.LENGTH_LONG).show();
populateVille(nompays);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}