1

我有一个 2 的应用程序Fragments。其中之一Fragments有 2 Spinners。用户在第一个中选择一个值,Spinner然后Adapter etc为第二个设置Spinner。问题是onSelectedItem事件永远不会被触发。这是我的代码(为简洁起见,删除了不相关的代码)。

public class RadarSelectionFragment extends Fragment implements AdapterView.OnItemSelectedListener {
    //Have also tried just plain OnItemSelectedListener

    public static final String ARG_SECTION_NUMBER = "section_number";

    public RadarSelectionFragment() {
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
            return inflater.inflate(R.layout.radars, container, false);
        } else {
            return container;
        }
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Spinner s = (Spinner) findViewById(R.id.stateselector);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.states, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
                    //I have tried setting the onItemSelectedListener(this) here, but it shows up in Eclipse as not being valid.
    }

    public void onViewCreated() {

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
        Toast.makeText(getActivity().getApplicationContext(), "onItemSelected() Triggered", 1).show();
        Spinner s = (Spinner) findViewById(R.id.stateselector);
        Spinner r = (Spinner) findViewById(R.id.townselector);
        ArrayAdapter<CharSequence> radapter;
        int selectedstate = 1;

        if (s.getItemAtPosition(pos).toString() == "New South Wales") {
            selectedstate = 1;
        } else if (s.getItemAtPosition(pos).toString() == "Victoria") {
            selectedstate = 2;
        } else if (s.getItemAtPosition(pos).toString() == "Queensland") {
            selectedstate = 3;
        } else ...

        switch (selectedstate) {
            case 1:
                radapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.states, android.R.layout.simple_spinner_item);
                radapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                r.setAdapter(radapter);
                break;
            case 2:
                radapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.states, android.R.layout.simple_spinner_item);
                radapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                r.setAdapter(radapter);
                break;

            ...
        }
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
} 
4

0 回答 0