English feliratok

← Modify ForecastAdapter - Quiz

Beágyazókód kérése
5 Languages

Showing Revision 4 created 05/24/2016 by Udacity Robot.

  1. Apparently the app shows dummy data
    that was hard coded into the XML file.
  2. Let's make the actual
    weather data appear on screen
  3. using the forecast adapter.
  4. For now we'll use the same layout for
    all list items, but
  5. we'll incorporate the today
    XML layout in the next video.
  6. Conceptually the forecast adapter
    manages the logics so that for
  7. each row of cursor we create
    corresponding list item.
  8. Think back on the explanation in
    lesson one about the array adapter,
  9. except instead of
    the datasource being an array,
  10. now the datasource is a cursor
    from the database The layout for
  11. the list item is defined in
    the list item forecast XML file.
  12. For one row of the cursor,
    we're going to go through and
  13. extract out the values.
  14. We call it binding the data to
    the views because we take the value in
  15. each column and
    we set it on to a different view.
  16. For example,
  17. the high temperature value would be set
    on to the high temperature text view.
  18. The low temperature value will be set
    onto the low temperature text view.
  19. For the next row,
    we bind the data in the same way
  20. until we create a list item for
    each row in the cursor.
  21. Currently, our code
    defines forecast adapter
  22. as extending from
    the cursor adapter class.
  23. If you look at the documentation for
  24. cursor adapter, you'll notice
    that it's an abstract class.
  25. There are two abstract methods that we
    need to override, bindView, and newView.
  26. The newView method returns a new list
    item layout, but has no data in it.
  27. The bindView method takes
    an existing list item layout and
  28. updates it,
    with the data from the cursor.
  29. In the forecast adapter
    class in the newView method
  30. we inflate the list item
    forecast XML layout.
  31. Then in the bindView method,
  32. we bind the data from the cursor
    to the individual views.
  33. For example, from the cursor we
    read out the weather description.
  34. Then we have to go through the view
    hierarchy to find the TextView
  35. that has the id
  36. Once we find the TextView, then we can
    update it with the right description.
  37. We've provided a gist of code for
  38. the bindView method,
    to help you get started.
  39. But we left some to-dos in there for
    you to do.
  40. In the gist, we also included some
    helper methods and strings for
  41. date formatting.
  42. That's because,
  43. in our current implementation, all
    the dates are displayed in the same way.
  44. In the wire frames, though,
    we have a more friendly date format.
  45. Such as today, tomorrow,
    Wednesday, Thursday, etc.
  46. Here's the logic for
  47. what the helper method does to
    get the friendly date string.
  48. If it's this week, it says today or
    tomorrow or the day of the week.
  49. If it's more than a week out, then we
    use the format Monday June 8 and so on.
  50. After you compile and
  51. run the app, the actual weather forecast
    data should now be showing in the app.
  52. And it's using a more
    friendly date format.
  53. Check these boxes when you're done.