1

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) 
        {
        }
    });
}
4

2 回答 2

1

我解决了我的问题!那是当我设置 spinner2 和 spinner3 以更改值时, spinner2.setOnItemSelectedListener() 也更改了 spinner3 值(只有当我自己在 spinner2 上选择一个值时才会更改)。所以 spinner2 写完了我在 spinner1 中设置的内容。我添加了一个 if 语句并修复了它。

于 2013-09-10T23:10:50.270 回答
0

我通过关注edittext解决了这个问题。edittext.requestfocus(),这使片段聚焦到 UI 并更新值。

于 2017-11-07T15:17:45.460 回答