2012-11-29 34 views
0

我想我在我的Android应用程序某处发生设计错误。我的(简体)代码粘贴在下面。从另一个类访问MainActivity中的方法

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

    } 

回答

0

我尝试了一些建议的解决方案,但可能无法让他们全部工作。

所以我解决它不使用一个单独的类是这样的:

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

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

+0

好吧,我会看一个教程上。 – dorien

+0

那么单独的类是否需要访问活动的上下文呢? – dorien

+0

将应用程序上下文传递给该类,如果它不是一个活动,并且它是一个活动,那么只需使用当前活动的上下文。 –

1

你可以创建一个Listener接口'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; 
    } 

} 
+0

Thanks。但是writeMidi方法仍然不能在侦听器中识别。 – dorien

0

2点的方式来做到这一点:

-首先将经过的MainActivity上下文班到CustomOnItemSelectedListener班。

-第二种方式是快速和肮脏,使writeMidi()方法静态,但是你应该记住,static methods可以只能访问static members不是非静态成员。

相关问题