我有一个类称为BuyCoins,在这一组内我有回调函数的java
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
TextView t=(TextView) findViewById(R.id.conversion);
CustomOnItemSelectedListener c = new CustomOnItemSelectedListener(t);
spinner1.setOnItemSelectedListener(c);
//String stockCode=c.getStock();
//Log.d(TAG,"message");
}
这产生其检测项目(产品)的新对象上的旋涂器中选择的方法addListenerOnSpinnerItemSelection()。 我想通过这回到BuyClass。我试图用注释掉的行来做到这一点,但是我收到的值是空的。
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
...
public String Stock;
...
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected=parent.getItemAtPosition(pos).toString();
switch(selected) {
case "20 Coins":
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + selected,
Toast.LENGTH_SHORT).show();
t.setText("$20");
this.setStock("20Coins");
break;
...
}
private void setStock(String s) {
Stock=s;
}
public String getStock() {
return Stock;
}
}
'我收到回值是null' - 如果你想送点东西回来你的方法必须有一个返回类型(你的是无效的,即没有),写一个值_into_一个已经作为参数传递的对象,或者将该值写入可在方法调用后读取的某个字段中。除非你知道你在做什么,否则我不会推荐选项2和3给你(可能有你无法处理的副作用)。 – Thomas
此外,如果您只是添加了监听器,那么'c.getStock();'能够返回_any_值? 'onItemSelected()'不能在那个时候被调用。 – Thomas
嗨,我不知道你在说什么.. String stockCode = c.getStock();返回null。 getStock方法有一个字符串返回类型 –