In using this code (adapted from here) I get the following error:
The constructor ArrayAdapter(new AdapterView.OnItemSelectedListener(){}, int, String
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;
}
}