Dynamic update of Spinner2 based on choice in Spinner 1

雨燕双飞 提交于 2019-12-02 06:13:39

In the constructor to your new ArrayAdapter on the following line:

ArrayAdapter<CharSequence> beAdapter = new ArrayAdapter<CharSequence>(this,

this points to the current instance of the OnItemSelectedListener class you are in and not the parent view, this is where the problem occurs because ArrayAdapter does not have a matching constructor. You should try using MyParentView.this (where MyParentView is the name of the view you are in) instead to pass the appropriate instance.

Yeah, here it is:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class DynamicSpinner extends Activity {
    private Spinner firstSpinner, secondSpinner;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        setAdapters();
        setListeners();
    }

private void findViews() {
    firstSpinner = (Spinner) findViewById(R.id.spLenses);
    secondSpinner = (Spinner) findViewById(R.id.spBE);
}

private void setAdapters() {
    ArrayAdapter<CharSequence> firstAdapter = ArrayAdapter.createFromResource(this, 
            R.array.lens_array, android.R.layout.simple_spinner_item);
    firstAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    firstSpinner.setAdapter(firstAdapter);
}

/* 
 * This is the important part. The user's choice in Spinner1 is passed to 
 * the procedure getDataForSecondSpinner () which creates an array shown in Spinner2  
 */
private void setListeners() {
    firstSpinner.setOnItemSelectedListener( new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String userChoiceSpinner1 =  (String) parent.getSelectedItem(); 
            String[] itemsSpinner2 = getDataForSecondSpinner (Integer.parseInt(userChoiceSpinner1)); 

            ArrayAdapter<CharSequence> secondAdapter = new ArrayAdapter<CharSequence>(DynamicSpinner.this, 
                    android.R.layout.simple_spinner_item, itemsSpinner2);
            secondAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);       
            secondSpinner.setAdapter(secondAdapter);                
        }
        public void onNothingSelected(AdapterView<?> parent) {
            secondSpinner.setAdapter(null);
        }
      }
    );

    secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        }
        public void onNothingSelected (AdapterView<?> parent) {
        }
      }
    );
}

/*
 * Returns an array based on user choice in Spinner 1 
 */
private String[] getDataForSecondSpinner (int userChoiceSpinnerOne) { 
    //some arbitrary parameters needed for my example, ignore...
    int param2 = 330;
    double param3 = 0.17;
    int array_index = 0;

    int inputUserChoice = userChoiceSpinnerOne;

    float param5 =  (float) ( (float) param2 / (float)userChoiceSpinnerOne  - 1); 
    int array_size = (int) (Math.round(param5  / param3) + 2);
    String[] arrayBE= new String[array_size];

    while (inputUserChoice <= param2) {
        inputUserChoice = (int) (userChoiceSpinnerOne * (param3 * array_index + 1));
        if (inputUserChoice <= param2) { 
            arrayBE[array_index] = String.valueOf(inputUserChoice);
        }
        else {
            arrayBE[array_index] = String.valueOf(param2);
        }
        array_index = array_index + 1;
        }

    return arrayBE;
}


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!