【发布时间】:2016-03-18 19:51:19
【问题描述】:
我正在开发一个 android 应用程序,其中有一个类 A。 A 类有一个微调器,有一个 B 类有微调器的 onItemSelected()、B implements OnItemSelectedListener 和 B 是公共的。
我想在A A时调用函数。因此,如果选择了first 项目,并将其更改为second,那么我想从A 类中调用一个函数。
我总是想调用相同的A 函数。它会刷新列表。
我该怎么做?
有类A 的微调器调用A 的onCreate:
public void addListenerOnSpinnerItemSelection() {
pspinner = (Spinner) findViewById(R.id.spinner1);
pspinner.setOnItemSelectedListener(new B());
}
还有B类:
public class B implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
Toast.makeText(parent.getContext(),
"The selected place: " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
A.refresh(); //I think I should call A's function like that but
// Android Studio tells meg that refresh() must be static
}
}
我应该在addListenerOnSpinnerItemSelection() 中调用A 的函数吗?还是在B 的onItemSelected 中?但是怎么做呢?
不幸的是,上面不是这样工作的。我如何在不使refresh()静态的情况下调用A的函数?
微调器从 xml 中获取其元素。
【问题讨论】:
标签: android spinner onitemselectedlistener