dorien
dorien

Reputation: 5387

Accessing a method in MainActivity, from another class

I think I am making a design error in my Android app somewhere. My (simplified) code is pasted below.

I am using the writeMidi method in MainActivity. However, I would also like to use it, or actually just trigger it, when "onItemSelected" is triggered in the custom listener.

I am a bit torn on how to do that. Should I redesign this code to fit the customlistener in the main activity?

Thanks for any help.

public class MainActivity extends Activity{


    int song = 0;
    int[] music;
    public int instrument;
    public CustomOnItemSelectedListener listener;


    // *******************************************************
    // set Layout - on create
    // *******************************************************
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        instrument = 0;

        listener = new CustomOnItemSelectedListener();
        addListenerOnSpinnerItemSelection();

        //more stuff, including using the writeMidi method

};


    public void addListenerOnSpinnerItemSelection(){

        instrumentSp = (Spinner) findViewById(R.id.instrument);
        instrumentSp.setOnItemSelectedListener(listener);
    }


    public void writeMidi(int[] music, int count) {
        // so some stff
    }

}

and in a separate file;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    private int instrument = 0;

      public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        Toast.makeText(parent.getContext(), 
            "Please wait a minute for the instrument to be changed. ", Toast.LENGTH_SHORT).show();
         instrument = pos;

      }


      public int getInstrument(){
          return instrument;
      }

    }

Upvotes: 0

Views: 1502

Answers (4)

dorien
dorien

Reputation: 5387

I tried a number of the solutions suggested, but could get none of them to fully work.

So I solved it by not using a separate class like this:

instrumentSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {   
     public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {

Upvotes: 0

rusmus
rusmus

Reputation: 1665

You could create a Listener interface 'InstrumentSelectedListener', or something like that. Then have your MainActivity implement that interface, register it as a listener on your CustomOnItemSelectedListener, and fire a 'writeMidiNow' event in your onItemSelected.

You would end up with something like:

public class MainActivity extends Activity implements OnInstrumentSelectedListener{


int song = 0;
int[] music;
public int instrument;
public CustomOnItemSelectedListener listener;


// *******************************************************
// set Layout - on create
// *******************************************************
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    instrument = 0;

    listener = new CustomOnItemSelectedListener();
    addListenerOnSpinnerItemSelection();

    //more stuff, including using the writeMidi method

};


public void addListenerOnSpinnerItemSelection(){

    instrumentSp = (Spinner) findViewById(R.id.instrument);
    instrumentSp.setOnItemSelectedListener(listener);
}

public void onInstrumentSelected(int instrument) {
    //  do some stuff with the instrument.
}

public void writeMidi(int[] music, int count) {
    // so some stff
}

}

And

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

public interface OnInstrumentSelectedListener{
    public void onInstrumentSelected(int instrument);
}

private int instrument = 0;
private OnInstrumentSelectedListener instrumentlistener;
  public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    Toast.makeText(parent.getContext(), 
        "Please wait a minute for the instrument to be changed. ", Toast.LENGTH_SHORT).show();
     instrument = pos;

     if(instrumentListener != null)
        instrumentListener.onInstrumentSelected(instrument);

  }

  public void setInstrumentListener(OnInstrumentSelectedListener listener) {
      this.instrumentListener = listener;
  }

  public int getInstrument(){
      return instrument;
  }

}

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

2 ways to do it:

- First will be passing the context of MainActivity class to CustomOnItemSelectedListener class.

- Second way is quick and dirty, make the writeMidi() method as static, but you should keep in mind that static methods can access only static members, Not non-static members.

Upvotes: 0

Ali Imran
Ali Imran

Reputation: 9217

Use broadcast receiver in main class and send different type of broadcast(Different messages) to activate different methods in main activity.

Upvotes: 1

Related Questions