private void detachSpinner() {
   if (isSpinnerAttached()) {
     if (mSpinnerContainer != null) {
       ((ViewGroup) mSpinnerContainer.getParent()).removeView(mSpinnerContainer);
       mSpinnerContainer = null;
       mSpinner = null;
       if (mSpinnerAdapter != null) {
         mSpinnerAdapter.swapData(null);
         mSpinnerAdapter = null;
       }
     }
   }
 }
 @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   Program program = (Program) mSpinnerAdapter.getItem(position);
   onProgramSelected(program.getId(), program.getName());
 }
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    listView = (ListView) view.findViewById(R.id.listview);
    View header =
        getLayoutInflater(savedInstanceState)
            .inflate(R.layout.fragment_programoverview_header, listView, false);
    mProgressBar = (ProgressBar) header.findViewById(R.id.progress_bar);
    mProgressBar.setVisibility(View.GONE);

    adapter = new ProgramStageAdapter(getLayoutInflater(savedInstanceState));
    listView.addHeaderView(header, CLASS_TAG, false);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);

    enrollmentLayout = (LinearLayout) header.findViewById(R.id.enrollmentLayout);
    enrollmentDateLabel = (TextView) header.findViewById(R.id.dateOfEnrollmentLabel);
    enrollmentDateValue = (TextView) header.findViewById(R.id.dateOfEnrollmentValue);
    incidentDateLabel = (TextView) header.findViewById(R.id.dateOfIncidentLabel);
    incidentDateValue = (TextView) header.findViewById(R.id.dateOfIncidentValue);
    profileCardView = (CardView) header.findViewById(R.id.profile_cardview);
    enrollmentCardview = (CardView) header.findViewById(R.id.enrollment_cardview);

    completeButton = (Button) header.findViewById(R.id.complete);
    terminateButton = (Button) header.findViewById(R.id.terminate);
    followupButton = (ImageButton) header.findViewById(R.id.followupButton);
    profileButton = (ImageButton) header.findViewById(R.id.profile_button);
    completeButton.setOnClickListener(null);
    terminateButton.setOnClickListener(null);
    followupButton.setOnClickListener(null);
    profileButton.setOnClickListener(this);
    profileCardView.setOnClickListener(this);

    missingEnrollmentLayout = (LinearLayout) header.findViewById(R.id.missingenrollmentlayout);
    newEnrollmentButton = (FloatingActionButton) header.findViewById(R.id.newenrollmentbutton);
    newEnrollmentButton.setOnClickListener(this);

    attribute1Label = (TextView) header.findViewById(R.id.headerItem1label);
    attribute1Value = (TextView) header.findViewById(R.id.headerItem1value);
    attribute2Label = (TextView) header.findViewById(R.id.headerItem2label);
    attribute2Value = (TextView) header.findViewById(R.id.headerItem2value);

    Bundle fragmentArguments = getArguments();
    Log.d(CLASS_TAG, "program: " + fragmentArguments.getString(PROGRAM_ID));

    attachSpinner();
    mSpinnerAdapter.swapData(
        MetaDataController.getProgramsForOrganisationUnit(
            fragmentArguments.getString(ORG_UNIT_ID),
            Program.SINGLE_EVENT_WITH_REGISTRATION,
            Program.MULTIPLE_EVENTS_WITH_REGISTRATION));

    if (savedInstanceState != null && savedInstanceState.getParcelable(STATE) != null) {
      mState = savedInstanceState.getParcelable(STATE);
    }

    if (mState == null) {
      mState = new ProgramOverviewFragmentState();
      OrganisationUnit ou =
          MetaDataController.getOrganisationUnit(fragmentArguments.getString(ORG_UNIT_ID));
      Program program = MetaDataController.getProgram(fragmentArguments.getString(PROGRAM_ID));
      mState.setOrgUnit(ou.getId(), ou.getLabel());
      mState.setProgram(program.getId(), program.getName());
      mState.setTrackedEntityInstance(fragmentArguments.getLong(TRACKEDENTITYINSTANCE_ID, -1));
    }

    onRestoreState(true);
  }