Implement multiple event listeners in the same fragment - Android
Asked Answered
T

2

15

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.

Toffey answered 4/8, 2013 at 23:9 Comment(2)
You can do both. Post your code, and explain your issue more please.Washery
Leo, I have now posted the code above....ThanksToffey
W
39

You can do:

public class FragmentTypeSelect extends Fragment 
        implements OnItemSelectedListener, OnClickListener {
Washery answered 5/8, 2013 at 20:6 Comment(3)
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.Toffey
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 ?Matrimonial
@Matrimonial 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.Washery
R
0
   public class Home extends AppCompatActivity  implements ImageAdapter.OnItemClickListener ,Filterable {

then click on the red bulb and implement the method. the implementation for nore than one interface can be done by seperating the interfaces with a comma.

Rexanna answered 8/12, 2019 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.