我有3个微调器,当第一个微调器更新时,第二个和第三个微调器应该更新,但只有第二个微调器正在更新。这两个检查确实打印到logcat,但第三个微调器没有被更新。如果我注释掉spinner2.setAdapter(),那么第三个微调器将更新。如果我更改顺序,那么第一个微调器应该先更新,然后第二个微调器仍然更新而第三个微调器不会更新。我哪里错了?
我正在使用以下代码:
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)
{
}
});
编辑: 因此,当应用程序加载时,它将显示“请选择学校”,它将显示所有部门和课程。当我点击第一个微调器并将其更改为“School 1”时。 spinner2应该更新为仅显示学校1中的部门(AEDepartments arrayadapter中的内容)和spinner3应该更新,因此只显示学校1中的课程(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)
{
}
});
}
答案 0 :(得分:1)
我解决了我的问题!当我设置spinner2和spinner3来改变值时,spinner2.setOnItemSelectedListener()也改变了spinner3值(当我自己在spinner2上选择一个值时,它应该只会改变)。所以spinner2过度写了我在spinner1中设置它的内容。我添加一个if语句并修复它。
答案 1 :(得分:0)
我通过聚焦edittext解决了这个问题。 edittext.requestfocus()
,这使得fragmet专注于UI并更新值。