android.support.v4.content.AsyncTaskLoader Java Examples

The following examples show how to use android.support.v4.content.AsyncTaskLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivityFragment.java    From CSCI4669-Fall15-Android with Apache License 2.0 6 votes vote down vote up
@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;
        }
    };
}
 
Example #2
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
    };

}
 
Example #3
Source File: BaseLoaderListFragment.java    From 4pdaClient-plus with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #4
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
    };

}
 
Example #5
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
    };

}
 
Example #6
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
        }
    };

}
 
Example #7
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #8
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #9
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #10
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #11
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #12
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #13
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };

}
 
Example #14
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #15
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #16
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
@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);
        }
    };
}
 
Example #17
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #18
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
@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);
        }
    };
}
 
Example #19
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
@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
        }
    };
}
 
Example #20
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };

}
 
Example #21
Source File: MainActivity.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    };

}
 
Example #22
Source File: BrickFragmentListBase.java    From 4pdaClient-plus with Apache License 2.0 votes vote down vote up
protected abstract AsyncTaskLoader<ListData> createLoader(int id, Bundle args);