下面列出了怎么用android.support.v4.content.AsyncTaskLoader的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Loader<List<Post>> onCreateLoader(int id, Bundle args) {
return new AsyncTaskLoader<List<Post>>(getContext()) {
@Override
public List<Post> loadInBackground() {
List<Post> data = new ArrayList<>();
try {
data = Api.getPosts();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
};
}
/**
* Instantiates and returns a new AsyncTaskLoader with the given ID.
* This loader will return task data as a Cursor or null if an error occurs.
*
* Implements the required callbacks to take care of loading data at all stages of loading.
*/
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<Cursor>(this) {
// Initialize a Cursor, this will hold all the task data
Cursor mTaskData = null;
// onStartLoading() is called when a loader first starts loading data
@Override
protected void onStartLoading() {
if (mTaskData != null) {
// Delivers any previously loaded data immediately
deliverResult(mTaskData);
} else {
// Force a new load
forceLoad();
}
}
// loadInBackground() performs asynchronous loading of data
@Override
public Cursor loadInBackground() {
// Will implement to load data
return null;
}
// deliverResult sends the result of the load, a Cursor, to the registered listener
public void deliverResult(Cursor data) {
mTaskData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiates and returns a new AsyncTaskLoader with the given ID.
* This loader will return task data as a Cursor or null if an error occurs.
*
* Implements the required callbacks to take care of loading data at all stages of loading.
*/
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<Cursor>(this) {
// Initialize a Cursor, this will hold all the task data
Cursor mTaskData = null;
// onStartLoading() is called when a loader first starts loading data
@Override
protected void onStartLoading() {
if (mTaskData != null) {
// Delivers any previously loaded data immediately
deliverResult(mTaskData);
} else {
// Force a new load
forceLoad();
}
}
// loadInBackground() performs asynchronous loading of data
@Override
public Cursor loadInBackground() {
// Will implement to load data
return null;
}
// deliverResult sends the result of the load, a Cursor, to the registered listener
public void deliverResult(Cursor data) {
mTaskData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiates and returns a new AsyncTaskLoader with the given ID.
* This loader will return task data as a Cursor or null if an error occurs.
*
* Implements the required callbacks to take care of loading data at all stages of loading.
*/
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<Cursor>(this) {
// Initialize a Cursor, this will hold all the task data
Cursor mTaskData = null;
// onStartLoading() is called when a loader first starts loading data
@Override
protected void onStartLoading() {
if (mTaskData != null) {
// Delivers any previously loaded data immediately
deliverResult(mTaskData);
} else {
// Force a new load
forceLoad();
}
}
// loadInBackground() performs asynchronous loading of data
@Override
public Cursor loadInBackground() {
// Will implement to load data
return null;
}
// deliverResult sends the result of the load, a Cursor, to the registered listener
public void deliverResult(Cursor data) {
mTaskData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiates and returns a new AsyncTaskLoader with the given ID.
* This loader will return task data as a Cursor or null if an error occurs.
*
* Implements the required callbacks to take care of loading data at all stages of loading.
*/
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<Cursor>(this) {
// Initialize a Cursor, this will hold all the task data
Cursor mTaskData = null;
// onStartLoading() is called when a loader first starts loading data
@Override
protected void onStartLoading() {
if (mTaskData != null) {
// Delivers any previously loaded data immediately
deliverResult(mTaskData);
} else {
// Force a new load
forceLoad();
}
}
// loadInBackground() performs asynchronous loading of data
@Override
public Cursor loadInBackground() {
// Will implement to load data
return null;
}
// deliverResult sends the result of the load, a Cursor, to the registered listener
public void deliverResult(Cursor data) {
mTaskData = data;
super.deliverResult(data);
}
};
}
protected AsyncTaskLoader<ListData> createLoader(int loaderId, Bundle args) {
if (loaderId == ItemsLoader.ID)
return new ItemsLoader(getActivity(), args, BaseLoaderListFragment.this::loadData);
else if (loaderId == CacheLoader.ID)
return new CacheLoader(getActivity(), (loaderId12, args12) -> {
ListData data = new ListData();
data.getItems().addAll(BaseLoaderListFragment.this.loadCache());
return data;
});
return null;
}
/**
* Instantiates and returns a new AsyncTaskLoader with the given ID.
* This loader will return task data as a Cursor or null if an error occurs.
*
* Implements the required callbacks to take care of loading data at all stages of loading.
*/
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<Cursor>(this) {
// Initialize a Cursor, this will hold all the task data
Cursor mTaskData = null;
// onStartLoading() is called when a loader first starts loading data
@Override
protected void onStartLoading() {
if (mTaskData != null) {
// Delivers any previously loaded data immediately
deliverResult(mTaskData);
} else {
// Force a new load
forceLoad();
}
}
// loadInBackground() performs asynchronous loading of data
@Override
public Cursor loadInBackground() {
// Will implement to load data
// COMPLETED (5) Query and load all task data in the background; sort by priority
// [Hint] use a try/catch block to catch any errors in loading data
try {
return getContentResolver().query(TaskContract.TaskEntry.CONTENT_URI,
null,
null,
null,
TaskContract.TaskEntry.COLUMN_PRIORITY);
} catch (Exception e) {
Log.e(TAG, "Failed to asynchronously load data.");
e.printStackTrace();
return null;
}
}
// deliverResult sends the result of the load, a Cursor, to the registered listener
public void deliverResult(Cursor data) {
mTaskData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiates and returns a new AsyncTaskLoader with the given ID.
* This loader will return task data as a Cursor or null if an error occurs.
*
* Implements the required callbacks to take care of loading data at all stages of loading.
*/
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<Cursor>(this) {
// Initialize a Cursor, this will hold all the task data
Cursor mTaskData = null;
// onStartLoading() is called when a loader first starts loading data
@Override
protected void onStartLoading() {
if (mTaskData != null) {
// Delivers any previously loaded data immediately
deliverResult(mTaskData);
} else {
// Force a new load
forceLoad();
}
}
// loadInBackground() performs asynchronous loading of data
@Override
public Cursor loadInBackground() {
// Will implement to load data
// Query and load all task data in the background; sort by priority
// [Hint] use a try/catch block to catch any errors in loading data
try {
return getContentResolver().query(TaskContract.TaskEntry.CONTENT_URI,
null,
null,
null,
TaskContract.TaskEntry.COLUMN_PRIORITY);
} catch (Exception e) {
Log.e(TAG, "Failed to asynchronously load data.");
e.printStackTrace();
return null;
}
}
// deliverResult sends the result of the load, a Cursor, to the registered listener
public void deliverResult(Cursor data) {
mTaskData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiates and returns a new AsyncTaskLoader with the given ID.
* This loader will return task data as a Cursor or null if an error occurs.
*
* Implements the required callbacks to take care of loading data at all stages of loading.
*/
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<Cursor>(this) {
// Initialize a Cursor, this will hold all the task data
Cursor mTaskData = null;
// onStartLoading() is called when a loader first starts loading data
@Override
protected void onStartLoading() {
if (mTaskData != null) {
// Delivers any previously loaded data immediately
deliverResult(mTaskData);
} else {
// Force a new load
forceLoad();
}
}
// loadInBackground() performs asynchronous loading of data
@Override
public Cursor loadInBackground() {
// Will implement to load data
// Query and load all task data in the background; sort by priority
// [Hint] use a try/catch block to catch any errors in loading data
try {
return getContentResolver().query(TaskContract.TaskEntry.CONTENT_URI,
null,
null,
null,
TaskContract.TaskEntry.COLUMN_PRIORITY);
} catch (Exception e) {
Log.e(TAG, "Failed to asynchronously load data.");
e.printStackTrace();
return null;
}
}
// deliverResult sends the result of the load, a Cursor, to the registered listener
public void deliverResult(Cursor data) {
mTaskData = data;
super.deliverResult(data);
}
};
}
@Override
public Loader<String> onCreateLoader(final int i, final Bundle bundle) {
// Within onCreateLoader
// COMPLETED (4) Return a new AsyncTaskLoader<String> as an anonymous inner class with this as the constructor's parameter
return new AsyncTaskLoader<String>(this) {
// COMPLETED (5) Override onStartLoading
@Override
protected void onStartLoading() {
// Within onStartLoading
// COMPLETED (6) If args is null, return.
if (bundle == null) {
return;
}
// COMPLETED (7) Show the loading indicator
mLoadingIndicator.setVisibility(View.VISIBLE);
// COMPLETED (8) Force a load
forceLoad();
// END - onStartLoading
}
// COMPLETED (9) Override loadInBackground
@Override
public String loadInBackground() {
// Within loadInBackground
// COMPLETED (10) Get the String for our URL from the bundle passed to onCreateLoader
String searchQueryUrlString = bundle.getString(SEARCH_QUERY_URL_EXTRA);
// COMPLETED (11) If the URL is null or empty, return null
if (searchQueryUrlString == null || searchQueryUrlString.isEmpty()) {
return null;
}
// COMPLETED (12) Copy the try / catch block from the AsyncTask's doInBackground method
try {
URL githubURL = new URL(searchQueryUrlString);
String githubSearchResults = NetworkUtils.getResponseFromHttpUrl(githubURL);
return githubSearchResults;
} catch (IOException e) {
e.printStackTrace();
return null;
}
// END - loadInBackground
}
};
}
@Override
public Loader<String> onCreateLoader(int id, final Bundle args) {
return new AsyncTaskLoader<String>(this) {
// COMPLETED (1) Create a String member variable called mGithubJson that will store the raw JSON
private String mGithubJson;
@Override
protected void onStartLoading() {
/* If no arguments were passed, we don't have a query to perform. Simply return. */
if (args == null) {
return;
}
/*
* When we initially begin loading in the background, we want to display the
* loading indicator to the user
*/
mLoadingIndicator.setVisibility(View.VISIBLE);
// COMPLETED (2) If mGithubJson is not null, deliver that result. Otherwise, force a load
if (mGithubJson != null) {
deliverResult(mGithubJson);
} else {
forceLoad();
}
}
@Override
public String loadInBackground() {
/* Extract the search query from the args using our constant */
String searchQueryUrlString = args.getString(SEARCH_QUERY_URL_EXTRA);
/* If the user didn't enter anything, there's nothing to search for */
if (searchQueryUrlString == null || TextUtils.isEmpty(searchQueryUrlString)) {
return null;
}
/* Parse the URL from the passed in String and perform the search */
try {
URL githubUrl = new URL(searchQueryUrlString);
String githubSearchResults = NetworkUtils.getResponseFromHttpUrl(githubUrl);
return githubSearchResults;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
// COMPLETED (3) Override deliverResult and store the data in mGithubJson
// COMPLETED (4) Call super.deliverResult after storing the data
@Override
public void deliverResult(String data) {
mGithubJson = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
URL weatherRequestUrl = NetworkUtils.getUrl(MainActivity.this);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
@Override
public Loader<String[]> onCreateLoader(int id, Bundle args) {
return new AsyncTaskLoader<String[]>(this) {
String[] mWeatherData = null;
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
@Override
public String[] loadInBackground() {
String locationQuery = SunshinePreferences.getPreferredWeatherLocation(MainActivity.this);
URL weatherRequestURL = NetworkUtils.buildUrl(locationQuery);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestURL);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
String locationQuery = SunshinePreferences
.getPreferredWeatherLocation(MainActivity.this);
URL weatherRequestUrl = NetworkUtils.buildUrl(locationQuery);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
URL weatherRequestUrl = NetworkUtils.getUrl(MainActivity.this);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
URL weatherRequestUrl = NetworkUtils.getUrl(MainActivity.this);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
URL weatherRequestUrl = NetworkUtils.getUrl(MainActivity.this);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
URL weatherRequestUrl = NetworkUtils.getUrl(MainActivity.this);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
String locationQuery = SunshinePreferences
.getPreferredWeatherLocation(MainActivity.this);
URL weatherRequestUrl = NetworkUtils.buildUrl(locationQuery);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
String locationQuery = SunshinePreferences
.getPreferredWeatherLocation(MainActivity.this);
URL weatherRequestUrl = NetworkUtils.buildUrl(locationQuery);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param loaderArgs Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<String[]> onCreateLoader(int id, final Bundle loaderArgs) {
return new AsyncTaskLoader<String[]>(this) {
/* This String array will hold and help cache our weather data */
String[] mWeatherData = null;
/**
* Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
*/
@Override
protected void onStartLoading() {
if (mWeatherData != null) {
deliverResult(mWeatherData);
} else {
mLoadingIndicator.setVisibility(View.VISIBLE);
forceLoad();
}
}
/**
* This is the method of the AsyncTaskLoader that will load and parse the JSON data
* from OpenWeatherMap in the background.
*
* @return Weather data from OpenWeatherMap as an array of Strings.
* null if an error occurs
*/
@Override
public String[] loadInBackground() {
URL weatherRequestUrl = NetworkUtils.getUrl(MainActivity.this);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Sends the result of the load to the registered listener.
*
* @param data The result of the load
*/
public void deliverResult(String[] data) {
mWeatherData = data;
super.deliverResult(data);
}
};
}
protected abstract AsyncTaskLoader<ListData> createLoader(int id, Bundle args);