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, R.id.list_item_forecast_textview, 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(R.id.listview_forecast); What does rootView refer to and why do you need it here?

The code is as follows:

package com.example.android.sunshine.app;  import android.os.Bundle; import android.support.v4.app.Fragment; 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, R.id.list_item_forecast_textview, weekForecast);          ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);         listView.setAdapter(mForestAdapter);          return rootView;     } 
Professor Asked on June 14, 2020 in Android langue,   XML.
Add Comment
2 Answer(s)

WordPress database error: [Not unique table/alias: 'order_vote']
SELECT SQL_CALC_FOUND_ROWS sofns_posts.ID FROM sofns_posts LEFT JOIN sofns_postmeta as order_vote ON order_vote.post_id = sofns_posts.ID AND ( order_vote.meta_key = 'et_vote_count' ) LEFT JOIN sofns_postmeta as order_best ON order_best.post_id = sofns_posts.ID AND ( order_best.meta_key = 'et_is_best_answer' ) LEFT JOIN sofns_postmeta as order_vote ON order_vote.post_id = sofns_posts.ID AND ( order_vote.meta_key = 'et_vote_count' ) LEFT JOIN sofns_postmeta as order_best ON order_best.post_id = sofns_posts.ID AND ( order_best.meta_key = 'et_is_best_answer' ) WHERE 1=1 AND sofns_posts.post_parent = 165808 AND sofns_posts.post_type = 'answer' AND (sofns_posts.post_status = 'publish') ORDER BY order_best.meta_value DESC, CAST(order_vote.meta_value AS SIGNED) DESC, sofns_posts.post_date ASC LIMIT 0, 10

Your Answer

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