2 のアプリケーションがありますFragments
。1 つFragments
は 2を持っていSpinners
ます。ユーザーは最初の値を選択し、これが 2 番目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
}
}