我檢查了這個問題的相關問題,並嘗試了一些建議,但似乎並沒有執行setOnItemSelectedListener
事件。這裏是我的活動代碼:微調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) {
}
});
我希望你的幫助。 謝謝!
是你的微調填充數據? – rafsanahmad007
是的我的微調組件填充數據,它只是當我嘗試選擇一個元素什麼也沒有發生 – MeknessiHamida