Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Commit 29db009

Browse files
author
Lyla
committed
3.10 Refactor fetching weather data so we don't have to stare at the dummy data
1 parent a01a072 commit 29db009

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
import java.net.URL;
4747
import java.text.SimpleDateFormat;
4848
import java.util.ArrayList;
49-
import java.util.Arrays;
50-
import java.util.List;
5149

5250
/**
5351
* Encapsulates fetching the forecast and displaying it as a {@link ListView} layout.
@@ -78,11 +76,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
7876
// as you specify a parent activity in AndroidManifest.xml.
7977
int id = item.getItemId();
8078
if (id == R.id.action_refresh) {
81-
FetchWeatherTask weatherTask = new FetchWeatherTask();
82-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
83-
String location = prefs.getString(getString(R.string.pref_location_key),
84-
getString(R.string.pref_location_default));
85-
weatherTask.execute(location);
79+
updateWeather();
8680
return true;
8781
}
8882
return super.onOptionsItemSelected(item);
@@ -92,27 +86,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
9286
public View onCreateView(LayoutInflater inflater, ViewGroup container,
9387
Bundle savedInstanceState) {
9488

95-
// Create some dummy data for the ListView. Here's a sample weekly forecast
96-
String[] data = {
97-
"Mon 6/23 - Sunny - 31/17",
98-
"Tue 6/24 - Foggy - 21/8",
99-
"Wed 6/25 - Cloudy - 22/17",
100-
"Thurs 6/26 - Rainy - 18/11",
101-
"Fri 6/27 - Foggy - 21/10",
102-
"Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
103-
"Sun 6/29 - Sunny - 20/7"
104-
};
105-
List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
106-
107-
// Now that we have some dummy forecast data, create an ArrayAdapter.
108-
// The ArrayAdapter will take data from a source (like our dummy forecast) and
89+
// The ArrayAdapter will take data from a source and
10990
// use it to populate the ListView it's attached to.
11091
mForecastAdapter =
11192
new ArrayAdapter<String>(
11293
getActivity(), // The current context (this activity)
11394
R.layout.list_item_forecast, // The name of the layout ID.
11495
R.id.list_item_forecast_textview, // The ID of the textview to populate.
115-
weekForecast);
96+
new ArrayList<String>());
11697

11798
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
11899

@@ -133,6 +114,20 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
133114
return rootView;
134115
}
135116

117+
private void updateWeather() {
118+
FetchWeatherTask weatherTask = new FetchWeatherTask();
119+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
120+
String location = prefs.getString(getString(R.string.pref_location_key),
121+
getString(R.string.pref_location_default));
122+
weatherTask.execute(location);
123+
}
124+
125+
@Override
126+
public void onStart() {
127+
super.onStart();
128+
updateWeather();
129+
}
130+
136131
public class FetchWeatherTask extends AsyncTask<String, Void, String[]> {
137132

138133
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();

0 commit comments

Comments
 (0)