I have a fragment which consists of a spinner and a button. You select one of four options with the spinner and then the button will take you to the next activity.
In order to implement the spinner I need to implement onItemSelectedListener on the Fragment but to use the button I need to implement onClickListener. But I can't do both??? I would have expected this to be a really simple thing and the need to have multiple different Event listeners on a View must be common, so how do you implement this?
Here is the code that I am using:-
public class FragmentTypeSelect extends Fragment implements OnItemSelectedListener {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState){
// set the view so that it can be referenced
View theView = inflater.inflate(R.layout.fragment_type_select,
container,false);
// set OnClickListener for the button
setUpClickListener(theView,R.id.but_select);
//===============================
// NEW TYPE SPINNER
//===============================
Spinner spinner = (Spinner) theView.findViewById(R.id.new_type_spinner);
spinner.setOnItemSelectedListener((OnItemSelectedListener) this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.types_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
return theView;
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id)
{
TextView headingText = (TextView)getView().findViewById(R.id.new_diet_type_text_heading);
TextView detailText = (TextView)getView().findViewById(R.id.new_diet_type_text_detail);
if (pos == 0)
{
headingText.setText(R.string.heading_type_1);
detailText.setText(R.string.detail_type_1);
}
if (pos == 1)
{
headingText.setText(R.string.heading_type_2);
detailText.setText(R.string.detail_type_2);
}
if (pos == 2)
{
headingText.setText(R.string.heading_type_3);
detailText.setText(R.string.detail_type_3);
}
if (pos == 3)
{
headingText.setText(R.string.heading_type_4);
detailText.setText(R.string.detail_type_4);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void onClick(View view){
Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_LONG).show();
}
private void setUpClickListener(View theView, int childViewID) {
View childView = theView.findViewById(childViewID);
childView.setOnClickListener((OnClickListener) this);
}
}
Originally I just had the spinner in and got this working fine. I then tried to put in the button function with the set OnClickListener in the onCreateView and adding the additional onClick and setUpClickListener methods. This is exactly how I have done it elsewhere but in other cases I have not had other events to handle and have made the class implement the onClickListener. Java does not support multiple interface implements (as I understand it) and hence my question.
Hope you can help. I'm probably being a bit thick but I am still quite new to the whole OO as well as Android.
You can do:
public class FragmentTypeSelect extends Fragment
implements OnItemSelectedListener, OnClickListener {