从另一个类访问MainActivity中的方法

时间:2012-11-29 09:24:58

标签: java android class android-activity listener

我认为我在某个地方的Android应用中出现了设计错误。我的(简化)代码粘贴在下面。

我在MainActivity中使用writeMidi方法。但是,当在自定义侦听器中触发“onItemSelected”时,我还想使用它,或者实际上只是触发它。

我对如何做到这一点有点不知所措。我应该重新设计此代码以适应主要活动中的customlistener吗?

感谢您的帮助。

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
    }

}

并在一个单独的文件中;

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;
      }

    }

4 个答案:

答案 0 :(得分:1)

在主类中使用广播接收器并发送不同类型的广播(不同消息)以激活主要活动中的不同方法。

答案 1 :(得分:1)

您可以创建一个Listener界面&#39; InstrumentSelectedListener&#39;或类似的东西。然后让您的MainActivity实现该接口,将其注册为CustomOnItemSelectedListener上的侦听器,并触发“writeMidiNow”#39; onItemSelected中的事件。

你最终会得到类似的东西:

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
}

}

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;
  }

}

答案 2 :(得分:0)

有两种方法:

- 首先将MainActivity类的上下文传递给CustomOnItemSelectedListener类。

- 第二种方式快速而又脏,将writeMidi()方法设为静态,但您应该记住static methods可以< strong>仅限访问 static members非静态成员。

答案 3 :(得分:0)

我尝试了一些建议的解决方案,但是没有一个能完全发挥作用。

所以我通过不使用像这样的单独的类来解决它:

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