0

I've tried using this link but I can't get it to work for a spinner and a radio button.

Is there a better or easier way to share a listener between various items of different varieties?

EDIT - I should have mentioned that the reason I want to share the listener is that I need to use various adapters that I'm having trouble using in other classes.

Community
  • 1
  • 1
KRL
  • 223
  • 3
  • 12

3 Answers3

3

You can define a class that implement listener for both Spinner and radio button,

create one instance of the class and then assign that instance to both the radio button and the spinner. For example:

package italialinux.example;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;


public class ButtonAction implements OnClickListener, OnItemSelectedListener {

    TextView btnLocalText;

    public ButtonAction(TextView tv) {
        super();
        btnLocalText = tv;
    }

    @Override
    public void onClick(View arg0) {
        btnLocalText.setText("Hello from a ButtonAction!!");
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}
Ivan
  • 4,186
  • 5
  • 39
  • 72
  • The problem is that I need to use various adapters that I'm having trouble using in other classes. – KRL Oct 07 '11 at 11:50
  • Fantastic example you've added there, thanks. For others as inept as myself this is what needs to be added to your buttons/spinners/misc to call the class; clr.setOnClickListener(new ButtonAction('whatever value')); – KRL Oct 07 '11 at 12:38
0

If you want them to perform the same function on the click, the simply add the same listner instance in the setOnClickListener() method to each view/widget. If not then you will have to detect which view was clicked and perform the required action accordingly

devanshu_kaushik
  • 929
  • 11
  • 30
0

i think the code example below is what you looking for

//the import for onClickListener you need is here (along with other imports --- you can get all of them by pressing ctrl+shift+o on Eclips IDE)

import android.view.View.OnClickListener;

public class MyActivity extends Activity implements OnClickListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Button myButton = (Button) findViewById(R.id.myButtonId);
    ImageView myImageView = (ImageView) findViewById(R.id.myImageViewId);
    RadioButton myRadioButton = (RadioButton) findViewById(R.id.myRadioButtonId);
    CheckBox myCheckBox = (CheckBox) findViewById(R.id.myCheckBoxId);

    myButton.setOnClickListener(this);
    myImageView.setOnClickListener(this);
    myRadioButton.setOnClickListener(this);
    myCheckBox.setOnClickListener(this);

}

public void onClick(View view) {

    switch (view.getId()) {

    case R.id.myButtonId:
        // do the work here for Button click listener
        break;

    case R.id.myImageViewId:
        // do the work here for Image click listener
        break;

    case R.id.myRadioButtonId:
        // do the work here for RadioButton click listener
        break;

    case R.id.myCheckBoxId:
        // do the work here for CheckBox click listener
        break;

    }

}

}

Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40