I want to Leverage Loaders in my App as
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
private ListView forecasteListView;
private SimpleCursorAdapter forecasteAdapter;
private String mLocation;
private static final int FORE_CAST_LOADER = 404;
private static final String[] FORE_CAST_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
};
public static final int COL_WEATHER_ID = 0;
public static final int COL_WEATHER_DATE = 1;
public static final int COL_WEATHER_DESC = 2;
public static final int COL_WEATHER_MAX_TEMP = 3;
public static final int COL_WEATHER_MIN_TEMP = 4;
public static final int COL_LOCATION_SETTING = 5;
public ForecastFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(FORE_CAST_LOADER, null, this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forcast_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_refresh){
updateWeather();
return true;
}
return super.onOptionsItemSelected(item);
}
private void updateWeather() {
FetchWeatherTask forecastWeather = new FetchWeatherTask(getActivity());
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String cityName = sharedPreferences.getString(getString(R.string.pref_city_key),getString(R.string.pref_city_default));
forecastWeather.execute(cityName, null, null);
}
@Override
public void onStart() {
super.onStart();
updateWeather();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
forecasteAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.list_item_forecast,
null,
new String[]{WeatherContract.WeatherEntry.COLUMN_DATE,
WeatherContract.WeatherEntry.COLUMN_SHORT_DESC,
WeatherContract.WeatherEntry.COLUMN_MAX_TEMP,
WeatherContract.WeatherEntry.COLUMN_MIN_TEMP
},
new int[]{R.id.list_item_date_textView,
R.id.list_item_forecast_desc_textView,
R.id.list_item_high_textView,
R.id.list_item_low_textView
}, 0
);
Log.e("onCreateView()", "List Adapter is of " + forecasteAdapter.getCount() + " size");
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
forecasteListView = (ListView) rootView.findViewById(R.id.listView_forecast);
return rootView ;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC";
mLocation = Utility.getPreferredLocation(getActivity());
Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(mLocation, System.currentTimeMillis());
Log.e("FORE_CAST_FRAGMENT", "Uri : " + weatherForLocationUri.toString() );
return new CursorLoader(getActivity(), weatherForLocationUri, FORE_CAST_COLUMNS, null, null, sortOrder);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.e("Loader<Cursor>", "Size of Cursor is " + data.getCount());
forecasteAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
forecasteAdapter.swapCursor(null);
}
}
the above code runs without errors/exceptions, first when App starts the method
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.e("Loader<Cursor>", "Size of Cursor is " + data.getCount());
forecasteAdapter.swapCursor(data);
}
prints
E/Loader<Cursor>﹕ Size of Cursor is 0
and it does not fetch data from database and populate ListView. Any help is greatly welcome in this case.
Disclaimer I'm flowing along with tutorials from Developing Android Apps - By Udacity course.