Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * 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 udatraining.dyomin.com.udaproj1; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import udatraining.dyomin.com.udaproj1.data.WeatherContract; import udatraining.dyomin.com.udaproj1.service.SunshineService; /** * Encapsulates fetching the forecast and displaying it as a {@link ListView} layout. */ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { public static final String[] FORECAST_COLUMNS = { WeatherContract.WeatherEntry.TABLE_NAME + "." + WeatherContract.WeatherEntry._ID, WeatherContract.WeatherEntry.COLUMN_DATE, WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, WeatherContract.LocationEntry.COLUMN_COORD_LAT, WeatherContract.LocationEntry.COLUMN_COORD_LONG, WeatherContract.WeatherEntry.COLUMN_HUMIDITY, WeatherContract.WeatherEntry.COLUMN_WIND_SPEED, WeatherContract.WeatherEntry.COLUMN_DEGREES, WeatherContract.WeatherEntry.COLUMN_PRESSURE }; static final int COL_WEATHER_ID = 0; static final int COL_WEATHER_DATE = 1; static final int COL_WEATHER_DESC = 2; static final int COL_WEATHER_MAX_TEMP = 3; static final int COL_WEATHER_MIN_TEMP = 4; static final int COL_LOCATION_SETTING = 5; static final int COL_WEATHER_CONDITION_ID = 6; static final int COL_COORD_LAT = 7; static final int COL_COORD_LONG = 8; static final int COL_WEATHER_HUMIDITY = 9; static final int COL_WEATHER_WIND_SPEED = 10; static final int COL_WEATHER_DEGREES = 11; static final int COL_WEATHER_PRESSURE = 12; private static final String LIST_POSITION = "listPosition"; private ForecastAdapter mForecastAdapter; private Callback callback; private int mPosition; private ListView listView; private boolean mUseTodayLayout; public ForecastFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Add this line in order for this fragment to handle menu events. setHasOptionsMenu(true); } public void setUseTodayLayout(boolean useTodayLayout) { mUseTodayLayout = useTodayLayout; if (mForecastAdapter != null) { mForecastAdapter.setmUseTodayLayout(useTodayLayout); } } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.forecastfragment, menu); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { if (mPosition != ListView.INVALID_POSITION) { savedInstanceState.putInt(LIST_POSITION, mPosition); } super.onSaveInstanceState(savedInstanceState); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_refresh) { updateWeather(); return true; } return super.onOptionsItemSelected(item); } @Override public void onActivityCreated(Bundle savedInstanceState) { getLoaderManager().initLoader(0, null, this); super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mForecastAdapter = new ForecastAdapter(getActivity(), null, 0); mForecastAdapter.setmUseTodayLayout(mUseTodayLayout); View rootView = inflater.inflate(R.layout.fragment_main, container, false); // Get a reference to the ListView, and attach this adapter to it. listView = (ListView) rootView.findViewById(R.id.listview_forecast); listView.setAdapter(mForecastAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Cursor cursor = (Cursor) parent.getItemAtPosition(position); if (cursor != null) { String locationSetting = Utility.getPreferredLocation(getActivity()); Uri weatherLocationWithDateUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate( locationSetting, cursor.getLong(ForecastFragment.COL_WEATHER_DATE)); callback = (Callback) getActivity(); callback.onItemSelected(weatherLocationWithDateUri); } mPosition = position; } }); if (null != savedInstanceState && savedInstanceState.containsKey(LIST_POSITION)) { mPosition = savedInstanceState.getInt(LIST_POSITION); } return rootView; } private void updateWeather() { Intent intent = new Intent(getActivity(), SunshineService.class); intent.putExtra(SunshineService.LOCATION_QUERY_EXTRA, Utility.getPreferredLocation(getActivity())); getActivity().startService(intent); } void onLocationChanged() { updateWeather(); getLoaderManager().restartLoader(0, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String locationSetting = Utility.getPreferredLocation(getActivity()); Uri uri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(locationSetting, System.currentTimeMillis()); String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; return new CursorLoader(getActivity(), uri, FORECAST_COLUMNS, null, null, sortOrder); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mForecastAdapter.swapCursor(data); if (mPosition != ListView.INVALID_POSITION) { listView.smoothScrollToPosition(mPosition); } } @Override public void onLoaderReset(Loader loader) { mForecastAdapter.swapCursor(null); } public interface Callback { public void onItemSelected(Uri dateUri); } }