===根本原因===
onItemSelected WORKS FINE,对于我在下面描述的问题,原因是我只是将一些代码分支到ArrayAdapter.notifyDataSetInvalidated()。给我的教训是:
* 当AdapterView上出现“无响应”时,请检查适配器。
感谢给我任何建议的朋友们。
===================
我想在一个片段中有一个微调器,但onItemSelected不起作用,请你告诉我原因,非常感谢。
这是活动布局xml:
<LinearLayout>
<!-- onItemSelected works fine when the spinnerConnector defined in activity layout -->
<Spinner
android:id="@+id/spinnerConnectors"
android:spinnerMode="dropdown" />
<!-- move spinnerConnectors into this fragment will cause the problem - onItemSelected not work
<fragment
android:id="@+id/fragmentConnectors"
android:name="com.sharework.transproxy.android.ConnectorsFragment"
tools:layout="@layout/list_fragment1" />
-->
</LinearLayout>
ConnectorsFragment
public class ConnectorsFragment extends android.app.Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create adapter ...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.connectors_fragment, null);
Spinner spinnerConnectors = (Spinner) view
.findViewById(R.id.spinnerConnectors);
spinnerConnectors.setAdapter(adapter);
return view;
}
@Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
Spinner spinnerConnectors = (Spinner) getView().findViewById(
R.id.spinnerConnectors);
spinnerConnectors
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// **** code to handle onItemSelected but not trigged
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
ConnectorsFragment的布局
<LinearLayout>
<Spinner
android:id="@+id/spinnerConnectors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
</LinearLayout>
活动
public class FactoryMonitorActivity TransProxyActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_factory_monitor);
// invoke ConnectorsFragment.start to query for connectors and fill into the adapter
}
}
Coule有人帮忙,谢谢!
答案 0 :(得分:0)
从Activity中删除与微调器相关的所有内容(可能您在测试期间错过了删除的内容),然后使spinnerConnectors
成为片段的类变量。因此,您必须只查找一次视图(此操作会对性能产生影响),并且下次使用微调器时,您不必在方法中声明局部变量。