Questions about android code from a Udacity course

I have several questions about the following code. If you would take the time to answer them, that would be awesome.

mForestAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,, weekForecast);. The object, mForestAdapter, takes four arguments. I am confused about the first three parameters.

  1. What does getActivity() mean, what does it do, and why is it used here?
  2. The tutorial indicated that I should create a new xml under the layout section called list_item_forecast.xml. And it is used as the third parameter. I’m not exactly sure why. Why create this new xml?
  3. The third parameter is the id of the textView that is inside of list_item_forecast.xml. What does this textView do? What influence does this textView have? I tried to change the xml code in here and it just clears everything from the preview.

ListView listView = (ListView) rootView.findViewById(; What does rootView refer to and why do you need it here?

The code is as follows:

package;  import android.os.Bundle; import; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView;  import java.util.ArrayList; import java.util.Arrays;   /**  * A placeholder fragment containing a simple view.  */ public class MainActivityFragment extends Fragment {      public MainActivityFragment() {     }      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,                              Bundle savedInstanceState) {         View rootView = inflater.inflate(R.layout.fragment_main, container, false);          ArrayAdapter<String> mForestAdapter;         String[] forecastArray = {                 "Today - Sunny - 88/63",                 "Tomorrow - Foggy - 70/46",                 "Wednesday - Cloudy - 72/63",                 "Thurs - Rainy - 64/51",                 "Fri - Foggy - 70/46",                 "Sat - Sunny - 76/68",                 "asdf",                 "asdfqwer",                 "qeprij",                 "qpweirj,"          };          ArrayList<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));          mForestAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,, weekForecast);          ListView listView = (ListView) rootView.findViewById(;         listView.setAdapter(mForestAdapter);          return rootView;     } 
Professor Asked on June 14, 2020 in Android langue,   XML.
Add Comment
2 Answer(s)

I don’t know how much you know about android development. “getActivity()” is a method from Fragment, and it’s called because the ArrayAdapter constructor requires a “Context” to be passed.

I guess “list_item_forecast.xml” is the layout of each list element. This is not a must, but if this way you can “inflate” each list element from this xml.

If you don’t understand anything of this you should first start with Google’s documentation :

Then read about list adapters and what they do. Good luck

Professor Answered on June 14, 2020.
Add Comment

You will be cool if you can read Android reference for each questions you have. For ArrayAdapter, you can refer here.

There are 6 constructors for ArrayAdapter and you can choose depend on your design and requirement. This is releted to your question.

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)


context The current context.

resource The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId The id of the TextView within the layout resource to be populated

objects The objects to represent in the ListView.

Professor Answered on June 14, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.