基于第一个微调器的选择填充第二个微调器

时间:2016-08-17 15:05:54

标签: java android

只要我在其中加载以下布局就会显示它已停止工作

public class AttendanceFragment extends android.support.v4.app.Fragment implements AdapterView.OnItemSelectedListener {

    View myView;
    Spinner s1, s2, s3;@
    Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.attendance, container, false);
        s1 = (Spinner) getView().findViewById(R.id.dept_spin);
        s2 = (Spinner) getView().findViewById(R.id.dept_sem);
        s3 = (Spinner) getView().findViewById(R.id.dept_subject);
        s1.setOnItemSelectedListener(this);
        s2.setOnItemSelectedListener(this);
        return myView;
    }

    @
    Override
    public void onItemSelected(AdapterView <? > parent, View view, int position, long id) {

        String sp1 = String.valueOf(s1.getSelectedItem());
        String sp2 = String.valueOf(s2.getSelectedItem());
        if (sp1.contentEquals("Computer Science Engineering")) {
            if (sp2.contentEquals("1")) {
                List < String > list = new ArrayList < String > ();
                list.add("Mathematics 1");
                list.add("Physics");
                list.add("Chemistry");
                list.add("Basic Electricals and Electronics");
                list.add("Engineering Thermodynamics");
                list.add("Computer Programming");
                list.add("Computer Programming Lab");
                list.add("Engineering Graphics Lab");
                list.add("Basic Electricals and Electronics Lab");

                ArrayAdapter < String > dataAdapter = new ArrayAdapter < String > (getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter.notifyDataSetChanged();
                s2.setAdapter(dataAdapter);
            }
            if (sp2.contentEquals("2")) {
                List < String > list = new ArrayList < String > ();
                list.add("Mathematics 2");
                list.add("Material Science");
                list.add("Environmental Science");
                list.add("Basic Civil and Mechanical");
                list.add("Engineering Mechanics");
                list.add("Communicative English");
                list.add("Physics Lab");
                list.add("Chemistry Lab");
                list.add("Workshop Lab");

                ArrayAdapter < String > dataAdapter = new ArrayAdapter < String > (getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter.notifyDataSetChanged();
                s2.setAdapter(dataAdapter);
            }
        }

    }

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

    }
}

0 个答案:

没有答案