Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2016 The Android Open Source Project
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package com.example.android.sunshine;
  17.  
  18. import android.content.Context;
  19. import android.support.v7.widget.RecyclerView;
  20. import android.view.LayoutInflater;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.widget.TextView;
  24.  
  25. // COMPLETED (15) Add a class called ForecastAdapter
  26. // COMPLETED (22) Extend RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder>
  27. /**
  28.  * {@link ForecastAdapter} exposes a list of weather forecasts to a
  29.  * {@link android.support.v7.widget.RecyclerView}
  30.  */
  31. public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder> {
  32.  
  33.     // COMPLETED (23) Create a private string array called mWeatherData
  34.     private String[] mWeatherData;
  35.  
  36.     // COMPLETED (47) Create the default constructor (we will pass in parameters in a later lesson)
  37.     public ForecastAdapter() {
  38.  
  39.     }
  40.  
  41.     // COMPLETED (16) Create a class within ForecastAdapter called ForecastAdapterViewHolder
  42.     // COMPLETED (17) Extend RecyclerView.ViewHolder
  43.     /**
  44.      * Cache of the children views for a forecast list item.
  45.      */
  46.     public class ForecastAdapterViewHolder extends RecyclerView.ViewHolder {
  47.  
  48.         // Within ForecastAdapterViewHolder ///////////////////////////////////////////////////////
  49.         // COMPLETED (18) Create a public final TextView variable called mWeatherTextView
  50.         public final TextView mWeatherTextView;
  51.  
  52.         // COMPLETED (19) Create a constructor for this class that accepts a View as a parameter
  53.         // COMPLETED (20) Call super(view)
  54.         // COMPLETED (21) Using view.findViewById, get a reference to this layout's TextView and save it to mWeatherTextView
  55.         public ForecastAdapterViewHolder(View view) {
  56.             super(view);
  57.             mWeatherTextView = (TextView) view.findViewById(R.id.tv_weather_data);
  58.         }
  59.         // Within ForecastAdapterViewHolder ///////////////////////////////////////////////////////
  60.     }
  61.  
  62.     // COMPLETED (24) Override onCreateViewHolder
  63.     // COMPLETED (25) Within onCreateViewHolder, inflate the list item xml into a view
  64.     // COMPLETED (26) Within onCreateViewHolder, return a new ForecastAdapterViewHolder with the above view passed in as a parameter
  65.     /**
  66.      * This gets called when each new ViewHolder is created. This happens when the RecyclerView
  67.      * is laid out. Enough ViewHolders will be created to fill the screen and allow for scrolling.
  68.      *
  69.      * @param viewGroup The ViewGroup that these ViewHolders are contained within.
  70.      * @param viewType  If your RecyclerView has more than one type of item (which ours doesn't) you
  71.      *                  can use this viewType integer to provide a different layout. See
  72.      *                  {@link android.support.v7.widget.RecyclerView.Adapter#getItemViewType(int)}
  73.      *                  for more details.
  74.      * @return A new ForecastAdapterViewHolder that holds the View for each list item
  75.      */
  76.     @Override
  77.     public ForecastAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
  78.         Context context = viewGroup.getContext();
  79.         int layoutIdForListItem = R.layout.forecast_list_item;
  80.         LayoutInflater inflater = LayoutInflater.from(context);
  81.         boolean shouldAttachToParentImmediately = false;
  82.  
  83.         View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
  84.         return new ForecastAdapterViewHolder(view);
  85.     }
  86.  
  87.     // COMPLETED (27) Override onBindViewHolder
  88.     // COMPLETED (28) Set the text of the TextView to the weather for this list item's position
  89.     /**
  90.      * OnBindViewHolder is called by the RecyclerView to display the data at the specified
  91.      * position. In this method, we update the contents of the ViewHolder to display the weather
  92.      * details for this particular position, using the "position" argument that is conveniently
  93.      * passed into us.
  94.      *
  95.      * @param forecastAdapterViewHolder The ViewHolder which should be updated to represent the
  96.      *                                  contents of the item at the given position in the data set.
  97.      * @param position                  The position of the item within the adapter's data set.
  98.      */
  99.     @Override
  100.     public void onBindViewHolder(ForecastAdapterViewHolder forecastAdapterViewHolder, int position) {
  101.         String weatherForThisDay = mWeatherData[position];
  102.         forecastAdapterViewHolder.mWeatherTextView.setText(weatherForThisDay);
  103.     }
  104.  
  105.     // COMPLETED (29) Override getItemCount
  106.     // COMPLETED (30) Return 0 if mWeatherData is null, or the size of mWeatherData if it is not null
  107.     /**
  108.      * This method simply returns the number of items to display. It is used behind the scenes
  109.      * to help layout our Views and for animations.
  110.      *
  111.      * @return The number of items available in our forecast
  112.      */
  113.     @Override
  114.     public int getItemCount() {
  115.         if (null == mWeatherData) return 0;
  116.         return mWeatherData.length;
  117.     }
  118.  
  119.     // COMPLETED (31) Create a setWeatherData method that saves the weatherData to mWeatherData
  120.     // COMPLETED (32) After you save mWeatherData, call notifyDataSetChanged
  121.     /**
  122.      * This method is used to set the weather forecast on a ForecastAdapter if we've already
  123.      * created one. This is handy when we get new data from the web but don't want to create a
  124.      * new ForecastAdapter to display it.
  125.      *
  126.      * @param weatherData The new weather data to be displayed.
  127.      */
  128.     public void setWeatherData(String[] weatherData) {
  129.         mWeatherData = weatherData;
  130.         notifyDataSetChanged();
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement