我认为我在某个地方的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;
}
}
答案 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) {