2017-03-17 135 views
0

我檢查了這個問題的相關問題,並嘗試了一些建議,但似乎並沒有執行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) { 

     } 
    }); 

我希望你的幫助。 謝謝!

+0

是你的微調填充數據? – rafsanahmad007

+0

是的我的微調組件填充數據,它只是當我嘗試選擇一個元素什麼也沒有發生 – MeknessiHamida

回答

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) { 

     } 
    }); 
} 
0

相反的:

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

嘗試;

pays.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
+0

你的建議沒有奏效 – MeknessiHamida

相關問題