【发布时间】:2015-10-26 10:07:38
【问题描述】:
我想在我的应用中利用加载器作为
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);
}
}
上面的代码运行没有错误/异常,首先当App启动方法时
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.e("Loader<Cursor>", "Size of Cursor is " + data.getCount());
forecasteAdapter.swapCursor(data);
}
打印
E/Loader<Cursor>﹕ Size of Cursor is 0
它不会从数据库中获取数据并填充 ListView。在这种情况下,我们非常欢迎任何帮助。
免责声明 我正在学习 Developing Android Apps - By Udacity 课程中的教程。
【问题讨论】:
-
或任何其他方式使用 uri 从 ContentPrivider 获取数据并为 List 填充适配器。