根据另一个微调器的选择填充微调器

时间:2017-04-02 19:17:43

标签: android spinner actionlistener

我必须根据所选国家/地区填充州。我还必须捕获在其他spinners中选择的项目。但是监听器方法没有被调用。

countriesSpinner.setOnItemSelectedListener(this);
statesSpinner.setOnItemSelectedListener(this);
yearSpinner.setOnItemSelectedListener(this);

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // On selecting a spinner item
    switch(view.getId()){
        case R.id.countriesSpinner:
        {
            Log.i("selected country",selectedCountry);
            selectedCountry = parent.getItemAtPosition(position).toString();
            populateStates();
            ArrayAdapter stateAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, states);
            statesSpinner.setAdapter(stateAdapter);
        }
        break;

        case R.id.statesSpinner:
        {
            selectedState=parent.getItemAtPosition(position).toString();
        }
        break;

        case R.id.yearSpinner:
        {
            selectedYear = parent.getItemAtPosition(position).toString();
        }
    }



}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

1 个答案:

答案 0 :(得分:0)

您的Switch ID错误了......您需要使用parent.getId()

试试这个:

 public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    switch (parent.getId()) 
    {         
        case R.id.countriesSpinner:

            Log.i("selected country",selectedCountry);
            selectedCountry = parent.getItemAtPosition(position).toString();
            populateStates();
            ArrayAdapter stateAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, states);
            statesSpinner.setAdapter(stateAdapter);
            break;              

        case R.id.statesSpinner:
            selectedState=parent.getItemAtPosition(position).toString();
            break;              
         case R.id.yearSpinner:
            selectedYear=parent.getItemAtPosition(position).toString();
            break;
    }
}