SUNSHINE APP : Details Activity

github: S0905-MoreDetails

* Reference: Udacity Nanodegree Program- Become an Android Developer *

To set up the Details Activity to use CursorLoaders to display more weather information in the Detail Layout.

Create a projection array of Strings and indices to help query weather details for that date!

  • Implement LoaderManager.LoaderCallbacks
    • onCreateLoader: Checks if the loader requested is our detail loader, return the appropriate CursorLoader.
    • onLoadFinished: Gets all the weather detail information from the cursor and displays them in the appropriate views.
    • onLoaderReset: Don’t do anything in it yet
  • Initialize the loader in DetailActivity’s onCreate
  • Update the ForecastAdapterOnClickHandler and refactor onClick to accept a long as its parameter.
  • In onClickpass the date from the cursor.
  • To refactor MainActivity’s onClick to build a URI for the clicked date and and pass it to the DetailActivty with the Intent using setData.

Note:

In this exercise, you’ve uses CursorLoaders to display more weather information in the Detail Layout.

Here’s the solution code for the onCreateLoader:

@Override
 public Loader<Cursor> onCreateLoader(int loaderId, Bundle loaderArgs) {
     switch (loaderId) {
         case ID_DETAIL_LOADER:
             return new CursorLoader(this,
                        mUri,
                        WEATHER_DETAIL_PROJECTION,
                        null,
                        null,
                        null);
         default:
             throw new RuntimeException("Loader Not Implemented: " + loaderId);
     }
 }

onLoadFinished should start by checking if cursor has valid data:

boolean cursorHasValidData = false;
if (data != null && data.moveToFirst()) {
   /* We have valid data, continue on to bind the data to the UI */
   cursorHasValidData = true;
}
if (!cursorHasValidData) {
   /* No data to display, simply return and do nothing */
   return;
}

Then for each piece of weather information, retrieve it from the cursor and display it in the appropriate view.

For example, the fist text view should the display the date as follows:

long localDateMidnightGmt = data.getLong(INDEX_WEATHER_DATE);
String dateText = SunshineDateUtils.getFriendlyDateString(this, localDateMidnightGmt, true);
mDateView.setText(dateText);

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s