Java tutorial
/** * Copyright 2014 Jay Soyer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sawyer.advadapters.app.adapters.jsonadapter; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import com.sawyer.advadapters.app.R; import com.sawyer.advadapters.app.ToastHelper; import com.sawyer.advadapters.app.adapters.AdapterBaseActivity; import com.sawyer.advadapters.app.data.MovieContent; import com.sawyer.advadapters.app.dialogs.AddJSONArrayDialogFragment; import org.json.JSONArray; import org.json.JSONObject; public class JSONAdapterActivity extends AdapterBaseActivity implements AddJSONArrayDialogFragment.EventListener, JSONAdapterFragment.EventListener { private static final String TAG_ADD_DIALOG_FRAG = "Tag Add Dialog Frag"; private static final String TAG_ADAPTER_FRAG = "Tag Adapter Frag"; private AddJSONArrayDialogFragment mAddDialogFragment; private JSONAdapterFragment mListFragment; @Override protected void clear() { mListFragment.getListAdapter().clear(); updateActionBar(); } @Override protected void clearAdapterFilter() { mListFragment.getListAdapter().getFilter().filter(""); } @Override protected String getInfoDialogMessage() { return getString(R.string.info_jsonadapter_message); } @Override protected String getInfoDialogTitle() { return getString(R.string.info_jsonadapter_title); } @Override protected int getListCount() { return mListFragment.getListAdapter().getCount(); } @Override protected void initFrags() { super.initFrags(); FragmentManager manager = getFragmentManager(); mListFragment = (JSONAdapterFragment) manager.findFragmentByTag(TAG_ADAPTER_FRAG); if (mListFragment == null) { mListFragment = JSONAdapterFragment.newInstance(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.frag_container, mListFragment, TAG_ADAPTER_FRAG); transaction.commit(); } mAddDialogFragment = (AddJSONArrayDialogFragment) manager.findFragmentByTag(TAG_ADD_DIALOG_FRAG); if (mAddDialogFragment != null) { mAddDialogFragment.setEventListener(this); } } @Override protected boolean isAddDialogEnabled() { return true; } @Override protected boolean isSearchViewEnabled() { return true; } @Override public void onAdapterCountUpdated() { updateActionBar(); } @Override public void onAddMultipleMoviesClick(JSONArray movies) { mListFragment.getListAdapter().addAll(movies); updateActionBar(); mAddDialogFragment.dismiss(); } @Override public void onAddSingleMovieClick(JSONObject movie) { mListFragment.getListAdapter().add(movie); updateActionBar(); mAddDialogFragment.dismiss(); } @Override public void onAddVarargsMovieClick(JSONObject... movies) { mListFragment.getListAdapter().addAll((Object[]) movies); updateActionBar(); mAddDialogFragment.dismiss(); } @Override public void onAttachFragment(Fragment fragment) { super.onAttachFragment(fragment); if (fragment instanceof JSONAdapterFragment) { mListFragment = (JSONAdapterFragment) fragment; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.jsonarray, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.menu_unit_test: Intent intent = new Intent(this, UnitTestJSONArrayActivity.class); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } @Override public boolean onQueryTextChange(String newText) { mListFragment.getListAdapter().getFilter().filter(newText); return true; } @Override public boolean onQueryTextSubmit(String query) { mListFragment.getListAdapter().getFilter().filter(query); return true; } @Override protected void reset() { mListFragment.getListAdapter().setJSONArray(MovieContent.ITEM_JSON); updateActionBar(); } @Override protected void sort() { ToastHelper.showSortNotSupported(this); } @Override protected void startAddDialog() { mAddDialogFragment = AddJSONArrayDialogFragment.newInstance(); mAddDialogFragment.setEventListener(this); mAddDialogFragment.show(getFragmentManager(), TAG_ADD_DIALOG_FRAG); } }