如何根据第一个Spinner项目选择来过滤第二个Spinner项目

时间:2019-02-25 10:02:20

标签: android arraylist spinner

我正在进行过滤器活动以过滤学生名单。过滤器有两个微调器-系和类。当活动打开时,它将从服务器加载所有部门和班级名称。到现在为止一切正常。现在我想如果用户选择任何部门,它将过滤类微调器的项目。我有两个微调器分别的模型类和arraylist。在班级模型中,我有部门名称,因此使用该名称我只想在与所选部门相关的那些班级上显示。

ArrayList<ClassModel> listClass = new ArrayList<>();
ArrayList<DepartmentModel> listDepartment = new ArrayList<>();

ArrayAdapter<ClassModel> adapterClass;
ArrayAdapter<DepartmentModel> adapterDepartment;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student_filter);

adapterClass = new ArrayAdapter<ClassModel>(this,android.R.layout.simple_spinner_item,listClass);
    adapterDepartment = new ArrayAdapter<DepartmentModel>(this,android.R.layout.simple_spinner_item,listDepartment);
    adapterClass.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapterDepartment.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spnClass.setAdapter(adapterClass);
    spnDepartment.setAdapter(adapterDepartment);

    listDepartment.add(new DepartmentModel("NA","View All"));
    listClass.add(new ClassModel("NA","View All","View All"));

    GetDepartment();
    GetClass();

     spnDepartment.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String dept_name = listDepartment.get(position).getDept();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
 }

  private class ClassModel{
    String id;
    String std;
    String dept;

    public ClassModel(String id, String std,String dept) {
        this.id = id;
        this.std = std;
        this.dept = dept;
    }

    public String getId() {
        return id;
    }

    public String getStd() {
        return std;
    }

    public String getDept() {
        return dept;
    }

    @NonNull
    @Override
    public String toString() {
        return this.std;
    }
}

private class DepartmentModel{
    String id;
    String dept;

    public DepartmentModel(String id, String dept) {
        this.id = id;
        this.dept = dept;
    }

    public String getId() {
        return id;
    }

    public String getDept() {
        return dept;
    }

    @NonNull
    @Override
    public String toString() {
        return this.dept;
    }
}

0 个答案:

没有答案