简体   繁体   中英

onItemSelected not trigged when it is included in a fragment

=== Root Reason ===

onItemSelected WORKS FINE, for issue I described below, the reason is I just branches some code to ArrayAdapter.notifyDataSetInvalidated(). The lesson to me is:

* When "No response" happens on AdapterView, check the adapter.

Thanks to the friends who gave me any advice.

===================

I want to have a spinner in a fragment, but the onItemSelected does not work, could you please tell me the reason, thanks a lot.

Here is the activity layout 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) {
                }
            });
}

}

layout of ConnectorsFragment

<LinearLayout>

<Spinner
    android:id="@+id/spinnerConnectors"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dropdown" />

</LinearLayout>

Activity

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 anyone help, thanks!

Remove everything related to the spinner from the Activity (maybe you missed to delete something during your test) and then make spinnerConnectors a class variable of the fragment. So you have to find for the view just one time (this operation has an impact on performances) and also next times you use the spinner you don't have to declare a local variable within a method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM