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

Commit

Permalink
1.04 Add dummy data
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent fab554b commit de032c1
Showing 1 changed file with 35 additions and 3 deletions.
@@ -1,14 +1,34 @@
/*
* Copyright (C) 2014 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.app;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class MainActivity extends ActionBarActivity {

Expand Down Expand Up @@ -55,7 +75,19 @@ public PlaceholderFragment() {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle savedInstanceState) {

// Create some dummy data for the ListView. Here's a sample weekly forecast
String[] data = {
"Mon 6/23 - Sunny - 31/17",
"Tue 6/24 - Foggy - 21/8",
"Wed 6/25 - Cloudy - 22/17",
"Thurs 6/26 - Rainy - 18/11",
"Fri 6/27 - Foggy - 21/10",
"Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
"Sun 6/29 - Sunny - 20/7"
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));

This comment has been minimized.

Copy link
@mayank-kgp

mayank-kgp Jun 2, 2015

what is line no. 92
I m not able to understand.

This comment has been minimized.

Copy link
@lswank

lswank Jun 3, 2015

gol197: Line 91 and Line 92 do the hard work of turning an XML-defined layout into actual objects. onCreateView returns a View. In this case, the view you are creating is defined in the fragment_main.xml file. R.layout.fragment_main is a reference to that particular file. You can learn more about the Inflater class in the Android documentation.

Also, if you are examining this source code while working on the Udacity course, a better option would be to use the Udacity forums. Traditionally line comments are meant for code reviews and less of a line-by-line explanation request. Good luck!

This comment has been minimized.

Copy link
@schwannden

schwannden Jul 11, 2015

Why use
List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
instead of
List<String> weekForecast = Arrays.asList(data);
directly?

This comment has been minimized.

This comment has been minimized.

Copy link
@artyhedgehog

artyhedgehog Oct 25, 2015

I don't see how is it an answer. I know there is a difference between Arrays.asList List implementation and ArrayList List implementation, but I do not see any reason to convert to ArrayList.
Actually, I also do not get what's the point of creating weekForecast variable which we do not use. Maybe I'll catch it later in the course...

UPD: as @DanielKaparunakis noted below, when we wrap into ArrayList, we can change the list size later - so it is an option to think of.

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
Expand Down

2 comments on commit de032c1

@DanielKaparunakis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is because if you use
List weekForecast = Arrays.asList(data);
the resulting weekForecast array will be fixed in size. If you use
List weekForecast = new ArrayList(Arrays.asList(data));
you can still change the size of the weekForecast array after adding the data.

Reference:
http://stackoverflow.com/questions/157944/create-arraylist-from-array

@artyhedgehog
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielKaparunakis, good point, thank you!
Though I don't think here is the case, it definitely can be in possible future, and so it is a good reason to wrap into ArrayList and maybe to have such habit at first.

Please sign in to comment.