Implement multiple event listeners in the same fragment - Android

41,861

You can do:

public class FragmentTypeSelect extends Fragment 
        implements OnItemSelectedListener, OnClickListener {
Share:
41,861
RobT
Author by

RobT

I currently work in a large Multinational designing changes to a large BackEnd System running over Oracle. I've taken up Android programming out of interest and to add another string to my bow.

Updated on December 09, 2020

Comments

  • RobT
    RobT over 3 years

    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.

    • Leo Landau
      Leo Landau almost 11 years
      You can do both. Post your code, and explain your issue more please.
    • RobT
      RobT almost 11 years
      Leo, I have now posted the code above....Thanks
  • RobT
    RobT almost 11 years
    That worked fine. I had tried this with a lower case onClickListener rather than OnClickListener and then when I searched for a solution everywhere seemed to say that you couldn't implement multiple interfaces separated by commas. Thanks for the clarification.
  • Xsmael
    Xsmael over 9 years
    I've also seen a lot, that in java it is not possible to implement several interfaces, but anyway is that a good practice ? do you recommand this way ?
  • Leo Landau
    Leo Landau over 9 years
    @Xsmael In Java you are allowed to implement multiple interfaces. And yes, I would say that this is a perfectly acceptable practice. Here you could use anonymous inner classes for the listeners too, but that's just a matter of code style, either style is fine in practice.