0

我想我在某个地方的 Android 应用程序中犯了一个设计错误。我的(简化的)代码粘贴在下面。

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

我对如何做到这一点感到有些不知所措。我应该重新设计此代码以适应主要活动中的自定义侦听器吗?

谢谢你的帮助。

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

4 回答 4

1

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

于 2012-11-29T09:29:16.940 回答
1

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

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

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

}
于 2012-11-29T09:29:51.183 回答
0

2种方法来做到这一点:

-首先将类的上下文传递MainActivityCustomOnItemSelectedListener类。

-第二种方法是快速而肮脏的,将writeMidi()方法设为静态,但你应该记住,static methods只能访问 static members,不能访问非静态成员。

于 2012-11-29T09:31:47.310 回答
0

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

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

instrumentSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {   
     public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
于 2012-12-05T15:56:15.107 回答