Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.example.android.sunshine;
- import android.content.Context;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- // COMPLETED (15) Add a class called ForecastAdapter
- // COMPLETED (22) Extend RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder>
- /**
- * {@link ForecastAdapter} exposes a list of weather forecasts to a
- * {@link android.support.v7.widget.RecyclerView}
- */
- public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder> {
- // COMPLETED (23) Create a private string array called mWeatherData
- private String[] mWeatherData;
- // COMPLETED (47) Create the default constructor (we will pass in parameters in a later lesson)
- public ForecastAdapter() {
- }
- // COMPLETED (16) Create a class within ForecastAdapter called ForecastAdapterViewHolder
- // COMPLETED (17) Extend RecyclerView.ViewHolder
- /**
- * Cache of the children views for a forecast list item.
- */
- public class ForecastAdapterViewHolder extends RecyclerView.ViewHolder {
- // Within ForecastAdapterViewHolder ///////////////////////////////////////////////////////
- // COMPLETED (18) Create a public final TextView variable called mWeatherTextView
- public final TextView mWeatherTextView;
- // COMPLETED (19) Create a constructor for this class that accepts a View as a parameter
- // COMPLETED (20) Call super(view)
- // COMPLETED (21) Using view.findViewById, get a reference to this layout's TextView and save it to mWeatherTextView
- public ForecastAdapterViewHolder(View view) {
- super(view);
- mWeatherTextView = (TextView) view.findViewById(R.id.tv_weather_data);
- }
- // Within ForecastAdapterViewHolder ///////////////////////////////////////////////////////
- }
- // COMPLETED (24) Override onCreateViewHolder
- // COMPLETED (25) Within onCreateViewHolder, inflate the list item xml into a view
- // COMPLETED (26) Within onCreateViewHolder, return a new ForecastAdapterViewHolder with the above view passed in as a parameter
- /**
- * This gets called when each new ViewHolder is created. This happens when the RecyclerView
- * is laid out. Enough ViewHolders will be created to fill the screen and allow for scrolling.
- *
- * @param viewGroup The ViewGroup that these ViewHolders are contained within.
- * @param viewType If your RecyclerView has more than one type of item (which ours doesn't) you
- * can use this viewType integer to provide a different layout. See
- * {@link android.support.v7.widget.RecyclerView.Adapter#getItemViewType(int)}
- * for more details.
- * @return A new ForecastAdapterViewHolder that holds the View for each list item
- */
- @Override
- public ForecastAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
- Context context = viewGroup.getContext();
- int layoutIdForListItem = R.layout.forecast_list_item;
- LayoutInflater inflater = LayoutInflater.from(context);
- boolean shouldAttachToParentImmediately = false;
- View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
- return new ForecastAdapterViewHolder(view);
- }
- // COMPLETED (27) Override onBindViewHolder
- // COMPLETED (28) Set the text of the TextView to the weather for this list item's position
- /**
- * OnBindViewHolder is called by the RecyclerView to display the data at the specified
- * position. In this method, we update the contents of the ViewHolder to display the weather
- * details for this particular position, using the "position" argument that is conveniently
- * passed into us.
- *
- * @param forecastAdapterViewHolder The ViewHolder which should be updated to represent the
- * contents of the item at the given position in the data set.
- * @param position The position of the item within the adapter's data set.
- */
- @Override
- public void onBindViewHolder(ForecastAdapterViewHolder forecastAdapterViewHolder, int position) {
- String weatherForThisDay = mWeatherData[position];
- forecastAdapterViewHolder.mWeatherTextView.setText(weatherForThisDay);
- }
- // COMPLETED (29) Override getItemCount
- // COMPLETED (30) Return 0 if mWeatherData is null, or the size of mWeatherData if it is not null
- /**
- * This method simply returns the number of items to display. It is used behind the scenes
- * to help layout our Views and for animations.
- *
- * @return The number of items available in our forecast
- */
- @Override
- public int getItemCount() {
- if (null == mWeatherData) return 0;
- return mWeatherData.length;
- }
- // COMPLETED (31) Create a setWeatherData method that saves the weatherData to mWeatherData
- // COMPLETED (32) After you save mWeatherData, call notifyDataSetChanged
- /**
- * This method is used to set the weather forecast on a ForecastAdapter if we've already
- * created one. This is handy when we get new data from the web but don't want to create a
- * new ForecastAdapter to display it.
- *
- * @param weatherData The new weather data to be displayed.
- */
- public void setWeatherData(String[] weatherData) {
- mWeatherData = weatherData;
- notifyDataSetChanged();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement