I have 3 spinners and when the first spinner is updated the 2nd and 3rd spinner should be updated, but only the 2nd spinner is being updated. The two checks do get printed to the logcat, but the third spinner is not being updated. If I comment out the spinner2.setAdapter() then the 3rd spinner will update. If I change the order so the 3rd spinner should be updated first then the 2nd spinner is still updated and the 3rd one isn't. Where am I going wrong?
I am using the code below:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
pos = arg2;
switch(pos)
{
case 0:
allDepts.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
spinner2.setAdapter(allDepts);
allCourses.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
spinner3.setAdapter(allCourses);
break;
case 1:
AEDepartments.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
AEDepartments.notifyDataSetChanged();
spinner2.setAdapter(AEDepartments);
Log.v("Spinner check", "Department check.");
AECourses.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
AECourses.notifyDataSetChanged();
spinner3.setAdapter(AECourses);
Log.v("Spinner check", "Course check.");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
EDIT: So when the app loads it will display "Please Select School" and it will display all the departments and courses. When I click on the first spinner and change it to "School 1". The spinner2 should be updated to only show the departments in school 1 (what's in the AEDepartments arrayadapter) and spinner3 should be updated so only courses in school 1 is shown (what's in the AECourses arrayadapter).
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.student1);
spinner1 = (Spinner)this.findViewById(R.id.school);
spinner2 = (Spinner)this.findViewById(R.id.dept);
spinner3 = (Spinner)this.findViewById(R.id.course);
ArrayAdapter<School> allSchools = new ArrayAdapter<School>(this,
android.R.layout.simple_spinner_item, new School[] {
new School("","-- Please Select School --"),
new School("A","School 1"),
new School("B","School 2"),
new School("C","School 3")
});
allSchools.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
spinner1.setAdapter(allSchools);
spinner1.setOnItemSelectedListener(this);
final ArrayAdapter<School> allDepts = new ArrayAdapter<School>(this,
android.R.layout.simple_spinner_item, new School[] {
new School("","-- Please Select Department --"),
new School("1","Dep1"),
new School("2","Dep2"),
new School("3","Dep3"),
new School("4","Dep4")
});
allDepts.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
spinner2.setAdapter(allDepts);
spinner2.setOnItemSelectedListener(this);
final ArrayAdapter<School> allCourses = new ArrayAdapter<School>(this,
android.R.layout.simple_spinner_item, new School[] {
new School("","-- Please Select Course --"),
new School("C1","Couyrse1"),
new School("C2","Couyrse2"),
new School("C3","Couyrse3"),
new School("C4","Couyrse4")
});
allCourses.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
spinner3.setAdapter(allCourses);
spinner3.setOnItemSelectedListener(this);
//Displaying all the departments in School 1
final ArrayAdapter<School> AEDepartments = new ArrayAdapter<School>(this,
android.R.layout.simple_spinner_item, new School[] {
new School("","-- Please Select Department --"),
new School("1","Dep1"),
new School("3","Dep3")
});
//Displaying all the courses in School 1
final ArrayAdapter<School> AECourses = new ArrayAdapter<School>(this,
android.R.layout.simple_spinner_item, new School[] {
new School("","-- Please Select Course --"),
new School("C1","Couyrse1"),
new School("C3","Couyrse3")
});
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
pos = arg2;
switch(pos)
{
case 0:
allDepts.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
allCourses.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
spinner2.setAdapter(allDepts);
spinner3.setAdapter(allCourses);
Log.v("Spinner check", "Department check.");
Log.v("Spinner check", "Course check.");
break;
case 1:
AEDepartments.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
AEDepartments.notifyDataSetChanged();
spinner2.setAdapter(AEDepartments);
Log.v("Spinner check", "Department check.");
AECourses.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
AECourses.notifyDataSetChanged();
spinner3.setAdapter(AECourses);
Log.v("Spinner check", "Course check.");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}