@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mChapterAdapter != null && mChapterAdapter.getCount() > 0) { outState.putParcelable(ARG_SELECTED_CHAPTER, mSelectedChapter); } }
private void addChapters(List<Chapter> chapterList) { Collections.sort(chapterList, mLocationComparator); mChapterAdapter.clear(); mChapterAdapter.addAll(chapterList); mChapterSpinnerView.setAdapter(mChapterAdapter); if (mSelectedChapter == null) { // if the location is available, select the first chapter by default. if (App.getInstance().getLastLocation() != null && chapterList.size() > 0) { mSelectedChapter = chapterList.get(0); } } if (mSelectedChapter != null) { mChapterSpinnerView.setText(mSelectedChapter.toString()); } else { mChapterSpinnerView.showDropDown(); mConfirmButton.setEnabled(false); } }
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mLocationComparator = new ChapterComparator( PrefUtils.getHomeChapterId(getActivity()), App.getInstance().getLastLocation()); mChapterAdapter = new ChapterAdapter(getActivity(), R.layout.spinner_item_welcome); mChapterAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); if (savedInstanceState != null) { mSelectedChapter = savedInstanceState.getParcelable(ARG_SELECTED_CHAPTER); } App.getInstance() .getModelCache() .getAsync( Const.CACHE_KEY_CHAPTER_LIST_HUB, new ModelCache.CacheListener() { @Override public void onGet(Object item) { Directory directory = (Directory) item; if (isContextValid()) { addChapters(directory.getGroups()); mLoadSwitcher.setDisplayedChild(1); } } @Override public void onNotFound(String key) { fetchChapters(); } }); mChapterSpinnerView.setThreshold(1); Filter.FilterListener enableConfirmOnUniqueFilterResult = new Filter.FilterListener() { @Override public void onFilterComplete(int count) { mConfirmButton.setEnabled(count == 1); if (count == 1) { mSelectedChapter = mChapterAdapter.getItem(0); updateAutoCompleteHint(mSelectedChapter); mChapterSpinnerView.dismissDropDown(); } else if (count == 0 && hasTrailingSpace(mChapterSpinnerView)) { mChapterSpinnerTextInputLayout.setError(getString(R.string.remove_trailing_spaces)); } else { resetAutoCompleteHint(); } } }; AdapterView.OnItemClickListener enableConfirmOnChapterClick = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mConfirmButton.setEnabled(true); mSelectedChapter = mChapterAdapter.getItem(position); updateAutoCompleteHint(mSelectedChapter); } }; mChapterSpinnerView.setFilterCompletionListener(enableConfirmOnUniqueFilterResult); mChapterSpinnerView.setOnItemClickListener(enableConfirmOnChapterClick); mChapterSpinnerView.setOnTouchListener(new ChapterSpinnerTouchListener()); mChapterSpinnerTextInputLayout.setErrorEnabled(true); mConfirmButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { if (getActivity() instanceof Step1Listener) { // TODO re-order cached chapter list ((Step1Listener) getActivity()).onConfirmedChapter(mSelectedChapter); } } }); }