Skip to content

Commit

Permalink
Sunshine S03.01-Exercise-RecyclerView
Browse files Browse the repository at this point in the history
 todo's through udacity#17
 todo's through udacity#46 - Exercise complete
  • Loading branch information
TheWiseOptimist committed May 23, 2018
1 parent 42dd191 commit 08ee63b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,103 @@
package com.example.android.sunshine;

// Within ForecastAdapter.java /////////////////////////////////////////////////////////////////

// TODO (22) Extend RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder>
// TODO (23) Create a private string array called mWeatherData
// TODO (47) Create the default constructor (we will pass in parameters in a later lesson)


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


// Within ForecastAdapter.java /////////////////////////////////////////////////////////////////
// TODO completed (22) Extend RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder>
// Within ForecastAdapter.java /////////////////////////////////////////////////////////////////
public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder> {

// TODO (47) Create the default constructor (we will pass in parameters in a later lesson)
public ForecastAdapter() {
}

// TODO completed (23) Create a private string array called mWeatherData
private String[] mWeatherData;

// TODO completed (24) Override onCreateViewHolder
@NonNull
@Override
public ForecastAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;

// TODO completed (25) Within onCreateViewHolder, inflate the list item xml into a view
// TODO completed (26) Within onCreateViewHolder, return a new ForecastAdapterViewHolder with the above view passed in as a parameter
// sort of condensed version
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.forecast_list_item,
parent,
false);
return new ForecastAdapterViewHolder(view);

// separated version
/*
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
int itemLayout = R.layout.forecast_list_item;
boolean initiallyAttach = false;
View view = inflater.inflate(itemLayout, parent, initiallyAttach);
return new ForecastAdapterViewHolder(view);
*/

// really condensed version
/*
return new ForecastAdapterViewHolder(
// view
LayoutInflater.from(parent.getContext())
.inflate(R.layout.forecast_list_item,
parent,
false)
);
*/

}

// TODO completed (27) Override onBindViewHolder
// TODO completed (28) Set the text of the TextView to the weather for this list item's position
@Override
public void onBindViewHolder(@NonNull ForecastAdapterViewHolder holder, int position) {

holder.mWeatherTextView.setText(mWeatherData[position]);
}

// TODO completed (29) Override getItemCount
// TODO completed (30) Return 0 if mWeatherData is null, or the size of mWeatherData if it is not null
@Override
public int getItemCount() {
return 0;
if (mWeatherData == null) return 0;
else return mWeatherData.length;
}

// TODO completed (31) Create a setWeatherData method that saves the weatherData to mWeatherData
// TODO completed (32) After you save mWeatherData, call notifyDataSetChanged
public void setWeatherData(String[] weatherData) {
mWeatherData = weatherData;
notifyDataSetChanged(); // built-in method in RecyclerView
}

// TODO completed (15) Add a class file called ForecastAdapter
// TODO completed (16) Create a class within ForecastAdapter called ForecastAdapterViewHolder
// TODO completed (17) Extend RecyclerView.ViewHolder
class ForecastAdapterViewHolder extends RecyclerView.ViewHolder {

// Within ForecastAdapterViewHolder ///////////////////////////////////////////////////////////
// TODO completed (18) Create a public final TextView variable called mWeatherTextView
public final TextView mWeatherTextView;

// TODO completed (19) Create a constructor for this class that accepts a View as a parameter
// TODO completed (20) Call super(view) within the constructor for ForecastAdapterViewHolder
// TODO completed (21) Using view.findViewById, get a reference to this layout's TextView and save it to mWeatherTextView
// Within ForecastAdapterViewHolder ///////////////////////////////////////////////////////////
public ForecastAdapterViewHolder(View itemView) {
super(itemView);
mWeatherTextView = itemView.findViewById(R.id.tv_weather_data);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -32,36 +34,15 @@
import java.net.URL;

public class MainActivity extends AppCompatActivity {

// Within ForecastAdapterViewHolder ///////////////////////////////////////////////////////////
// TODO (18) Create a public final TextView variable called mWeatherTextView

// TODO (19) Create a constructor for this class that accepts a View as a parameter
// TODO (20) Call super(view) within the constructor for ForecastAdapterViewHolder
// TODO (21) Using view.findViewById, get a reference to this layout's TextView and save it to mWeatherTextView
// Within ForecastAdapterViewHolder ///////////////////////////////////////////////////////////

// TODO completed (33) Delete mWeatherTextView
// private TextView mWeatherTextView;

// TODO (24) Override onCreateViewHolder
// TODO (25) Within onCreateViewHolder, inflate the list item xml into a view
// TODO (26) Within onCreateViewHolder, return a new ForecastAdapterViewHolder with the above view passed in as a parameter

// TODO (27) Override onBindViewHolder
// TODO (28) Set the text of the TextView to the weather for this list item's position

// TODO (29) Override getItemCount
// TODO (30) Return 0 if mWeatherData is null, or the size of mWeatherData if it is not null

// TODO (31) Create a setWeatherData method that saves the weatherData to mWeatherData
// TODO (32) After you save mWeatherData, call notifyDataSetChanged
// Within ForecastAdapter.java /////////////////////////////////////////////////////////////////


// TODO (33) Delete mWeatherTextView
private TextView mWeatherTextView;

// TODO (34) Add a private RecyclerView variable called mRecyclerView
// TODO (35) Add a private ForecastAdapter variable called mForecastAdapter
// TODO completed (34) Add a private RecyclerView variable called mRecyclerView
// TODO completed (35) Add a private ForecastAdapter variable called mForecastAdapter
private RecyclerView mRecyclerView;
private ForecastAdapter mForecastAdapter;

private TextView mErrorMessageDisplay;

Expand All @@ -72,27 +53,34 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forecast);

// TODO (36) Delete the line where you get a reference to mWeatherTextView
/*
* Using findViewById, we get a reference to our TextView from xml. This allows us to
* do things like set the text of the TextView.
*/
mWeatherTextView = (TextView) findViewById(R.id.tv_weather_data);
// TODO completed (36) Delete the line where you get a reference to mWeatherTextView
// mWeatherTextView = (TextView) findViewById(R.id.tv_weather_data);

// TODO (37) Use findViewById to get a reference to the RecyclerView
// TODO completed (37) Use findViewById to get a reference to the RecyclerView
mRecyclerView = findViewById(R.id.recyclerview_forecast);

/* This TextView is used to display errors and will be hidden if there are no errors */
mErrorMessageDisplay = (TextView) findViewById(R.id.tv_error_message_display);

// TODO (38) Create layoutManager, a LinearLayoutManager with VERTICAL orientation and shouldReverseLayout == false
// TODO completed (38) Create layoutManager, a LinearLayoutManager with VERTICAL orientation and shouldReverseLayout == false
LinearLayoutManager layoutManager = new LinearLayoutManager(
this, LinearLayoutManager.VERTICAL, false);

// TODO (39) Set the layoutManager on mRecyclerView
// TODO completed (39) Set the layoutManager on mRecyclerView
mRecyclerView.setLayoutManager(layoutManager);

// TODO (40) Use setHasFixedSize(true) on mRecyclerView to designate that all items in the list will have the same size
// TODO completed (40) Use setHasFixedSize(true) on mRecyclerView to designate that all items in the list will have the same size
mRecyclerView.setHasFixedSize(true);

// TODO (41) set mForecastAdapter equal to a new ForecastAdapter
// TODO completed (41) set mForecastAdapter equal to a new ForecastAdapter
mForecastAdapter = new ForecastAdapter();

// TODO (42) Use mRecyclerView.setAdapter and pass in mForecastAdapter
// TODO completed (42) Use mRecyclerView.setAdapter and pass in mForecastAdapter
mRecyclerView.setAdapter(mForecastAdapter);

/*
* The ProgressBar that will indicate to the user that we are loading data. It will be
Expand Down Expand Up @@ -128,9 +116,9 @@ private void loadWeatherData() {
private void showWeatherDataView() {
/* First, make sure the error is invisible */
mErrorMessageDisplay.setVisibility(View.INVISIBLE);
// TODO (43) Show mRecyclerView, not mWeatherTextView
// TODO completed (43) Show mRecyclerView, not mWeatherTextView
/* Then, make sure the weather data is visible */
mWeatherTextView.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.VISIBLE);
}

/**
Expand All @@ -141,9 +129,9 @@ private void showWeatherDataView() {
* need to check whether each view is currently visible or invisible.
*/
private void showErrorMessage() {
// TODO (44) Hide mRecyclerView, not mWeatherTextView
// TODO completed (44) Hide mRecyclerView, not mWeatherTextView
/* First, hide the currently visible data */
mWeatherTextView.setVisibility(View.INVISIBLE);
mRecyclerView.setVisibility(View.INVISIBLE);
/* Then, show the error */
mErrorMessageDisplay.setVisibility(View.VISIBLE);
}
Expand Down Expand Up @@ -187,15 +175,16 @@ protected void onPostExecute(String[] weatherData) {
mLoadingIndicator.setVisibility(View.INVISIBLE);
if (weatherData != null) {
showWeatherDataView();
// TODO (45) Instead of iterating through every string, use mForecastAdapter.setWeatherData and pass in the weather data
// TODO completed (45) Instead of iterating through every string, use mForecastAdapter.setWeatherData and pass in the weather data
/*
* Iterate through the array and append the Strings to the TextView. The reason why we add
* the "\n\n\n" after the String is to give visual separation between each String in the
* TextView. Later, we'll learn about a better way to display lists of data.
*/
for (String weatherString : weatherData) {
mWeatherTextView.append((weatherString) + "\n\n\n");
}
// for (String weatherString : weatherData) {
// mWeatherTextView.append((weatherString) + "\n\n\n");
// }
mForecastAdapter.setWeatherData(weatherData);
} else {
showErrorMessage();
}
Expand All @@ -217,8 +206,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_refresh) {
// TODO (46) Instead of setting the text to "", set the adapter to null before refreshing
mWeatherTextView.setText("");
// TODO completed (46) Instead of setting the text to "", set the adapter to null before refreshing
// mWeatherTextView.setText("");
mForecastAdapter.setWeatherData(null);
loadWeatherData();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>
android:layout_height="match_parent"/>

<TextView
android:id="@+id/tv_error_message_display"
Expand Down

0 comments on commit 08ee63b

Please sign in to comment.