Working in fragments, I'm trying to return a value determined from selecting an item in a spinner and I can't make it work. The Toast notification inside the method isn't even working, it seems like the problem is within the method. How can I fix this?
public Spinner districtSpinner, countySpinner;
public int selectedDistrict;
are the variables used. Here's my parent spinner method, districtSpinner()
public void districtSpinner() {
ArrayAdapter<String> Dadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.districtNames));
Dadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
districtSpinner.setAdapter(Dadapter);
districtSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
selectedDistrictPosition = districtSpinner.getSelectedItemPosition();
switch (selectedDistrictPosition) {
case 0: {
selectedDistrict = 1;
break;
}
case 1: {
selectedDistrict=2;
break;
}
case 2: {
selectedDistrict=3;
break;
}
case 3: {
selectedDistrict=4;
break;
}
case 4: {
selectedDistrict=5;
break;
}
case 5: {
selectedDistrict=6;
break;
}
case 6: {
selectedDistrict=8;
break;
}
case 7: {
selectedDistrict=9;
break;
}
case 8: {
selectedDistrict=10;
break;
}
case 9: {
selectedDistrict=11;
break;
}
case 10: {
selectedDistrict=12;
break;
}
default: {
selectedDistrict=1;
break;
}
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
}
this is the child spinner "countySpinner" that I am trying to make dependent on the parent spinner. The method should switch based on the selectedDistrict and set up the spinner with the corresponding string array containing each district's set of counties (R.array.districtx). Currently the second spinner does nothing, and as far as I'm concerned the countySpinner() isn't even called upon.
public void countySpinner() {
switch (selectedDistrict) {
case 0: {
break;
}
case 1: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district1));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 2: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district2));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 3: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district3));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 4: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district4));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 5: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district5));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 6: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district6));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 7: {
break;
}
case 8: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district8));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 9: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district9));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 10: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district10));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 11: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district11));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
case 12: {
ArrayAdapter<String> Cadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.district12));
Cadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
countySpinner.setAdapter(Cadapter);
countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
);
break;
}
default: {
break;
}
}
You should see a Toast as far as i can see. But it does not make sense to let it return a value. You are only setting an adapter in that function. Indeed there is an onclicklistener and in on click selectedDistrict is set. But that only happens when the user clicks an item. Your function has long returned by then. Your function now returns with the static selectedDistrict value which it had before you called it.
You could have stated the problem better like
I want to setup my second spinner according to the choice made in first spinner
. Then you better do like this as you have way to much equal code in it.