Advertisement
yo2man

Setting the data, and a lot of shit

Jul 17th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.59 KB | None | 0 0
  1. // Setting the Data
  2.  
  3. //Notes: We use the ViewHolder  pattern for custom list adapters because its an efficient way of reusing views that make for smooth scrolling
  4.  
  5.  
  6. /* Which adapter method is called to create the layout for each item being adapted?
  7. A. getItem()
  8. B. getCount()
  9. C. getView() <--answer
  10.  
  11. /*
  12. Which parameter of the getView() method is used to get the data object to match the specific view in the list that is being displayed?
  13.  
  14. Int Positon <-- answer from: " public View getView(int position, View convertView, ViewGroup parent) {            }"
  15. */
  16.  
  17.  
  18.  
  19.  
  20. //--------------------------------------------------------------------------------
  21. Forecast.java
  22.  
  23. package com.teamtreehouse.stormy.weather;
  24.  
  25. import com.teamtreehouse.stormy.R;
  26.  
  27. public class Forecast {
  28.     private Current mCurrent;
  29.     private Hour[] mHourlyForecast;
  30.     private Day[] mDailyForecast;
  31.  
  32.     public Current getCurrent() {
  33.         return mCurrent;
  34.     }
  35.  
  36.     public void setCurrent(Current current) {
  37.         mCurrent = current;
  38.     }
  39.  
  40.     public Day[] getDailyForecast() {
  41.         return mDailyForecast;
  42.     }
  43.  
  44.     public void setDailyForecast(Day[] dailyForecast) {
  45.         mDailyForecast = dailyForecast;
  46.     }
  47.  
  48.     public Hour[] getHourlyForecast() {
  49.         return mHourlyForecast;
  50.     }
  51.  
  52.     public void setHourlyForecast(Hour[] hourlyForecast) {
  53.         mHourlyForecast = hourlyForecast;
  54.     }
  55.  
  56.     //Copy the code from the getIconId method and paste it here. This way both DailyForecastActivity.java and Current.java can just say "return Forecast.getIconId()" to get the method and we wouldn't have to repeat ourselves
  57.     public static int getIconId(String iconString){
  58.  
  59.         // clear-day, clear-night, rain, snow, sleet, wind, fog, cloudy, partly-cloudy-day, or partly-cloudy-night
  60.         int iconId = R.drawable.clear_day; //set this one in default, forecast api documentation says to "set a sensible icon as default"
  61.  
  62.         if(iconString.equals("clear-day")){
  63.             iconId = R.drawable.clear_day; //notice that the ids have _ compared to string "-" because we arent allowed to have dashes as resource names in Android Studio
  64.         }
  65.         else if (iconString.equals("clear-night")){
  66.             iconId = R.drawable.clear_night;
  67.         }
  68.         else if (iconString.equals("rain")) {
  69.             iconId = R.drawable.rain;
  70.         }
  71.         else if (iconString.equals("snow")) {
  72.             iconId = R.drawable.snow;
  73.         }
  74.         else if (iconString.equals("sleet")) {
  75.             iconId = R.drawable.sleet;
  76.         }
  77.         else if (iconString.equals("wind")) {
  78.             iconId = R.drawable.wind;
  79.         }
  80.         else if (iconString.equals("fog")) {
  81.             iconId = R.drawable.fog;
  82.         }
  83.         else if (iconString.equals("cloudy")) {
  84.             iconId = R.drawable.cloudy;
  85.         }
  86.         else if (iconString.equals("partly-cloudy-day")) {
  87.             iconId = R.drawable.partly_cloudy;
  88.         }
  89.         else if (iconString.equals("partly-cloudy-night")) {
  90.             iconId = R.drawable.cloudy_night;
  91.         }
  92.  
  93.         return iconId;
  94.  
  95.     }
  96.  
  97. }
  98.  
  99.  
  100. //-------------------------------------------------------------------------
  101.  
  102. Current.java
  103.  
  104. package com.teamtreehouse.stormy.weather;
  105.  
  106. import com.teamtreehouse.stormy.R;
  107.  
  108. import java.text.SimpleDateFormat;
  109. import java.util.Date;
  110. import java.util.TimeZone;
  111.  
  112. public class Current {
  113.  
  114.     private String mIcon; // The Icon // it comes from Forecast API as a string, we convert it to an int
  115.     private long mTime; //Time
  116.     private double mTemperature; //Temp
  117.     private double mHumidity; //humidity
  118.     private double mPrecipChance; //chance of precipitation
  119.     private String mSummary; //summary at the bottom
  120.  
  121.     private String mTimeZone;
  122.  
  123.     public String getTimeZone() {
  124.         return mTimeZone;
  125.     }
  126.  
  127.     public void setTimeZone(String timeZone) {
  128.         mTimeZone = timeZone;
  129.     }
  130.  
  131.     // Step 2: Generate getters and setters for everything: Code>Generate...>Getters and Setters:
  132.     public double getHumidity() {
  133.         return mHumidity;
  134.     }
  135.  
  136.     public void setHumidity(double humidity) {
  137.         mHumidity = humidity;
  138.     }
  139.  
  140.     public String getIcon() {
  141.         return mIcon;
  142.     }
  143.     // now we want to add a new method to our model that gets the appropriate image
  144.     // based on the icon value we are getting from the forecast API.
  145.     // To get the image, we need to int ID that gets generated for each drawable resource.
  146.  
  147.     public int getIconId(){
  148.  
  149.         return Forecast.getIconId(mIcon); //return method called "getIconId" in the Forecast(.java) class (pass along Icon)
  150.  
  151. /* //get rid of this by moving it to Forecast.java and following the don't repeat yourself principle
  152.         // clear-day, clear-night, rain, snow, sleet, wind, fog, cloudy, partly-cloudy-day, or partly-cloudy-night
  153.         int iconId = R.drawable.clear_day; //set this one in default, forecast api documentation says to "set a sensible icon as default"
  154.  
  155.         if(iconString.equals("clear-day")){
  156.             iconId = R.drawable.clear_day; //notice that the ids have _ compared to string "-" because we arent allowed to have dashes as resource names in Android Studio
  157.         }
  158.         else if (iconString.equals("clear-night")){
  159.             iconId = R.drawable.clear_night;
  160.         }
  161.         else if (iconString.equals("rain")) {
  162.             iconId = R.drawable.rain;
  163.         }
  164.         else if (iconString.equals("snow")) {
  165.             iconId = R.drawable.snow;
  166.         }
  167.         else if (iconString.equals("sleet")) {
  168.             iconId = R.drawable.sleet;
  169.         }
  170.         else if (iconString.equals("wind")) {
  171.             iconId = R.drawable.wind;
  172.         }
  173.         else if (iconString.equals("fog")) {
  174.             iconId = R.drawable.fog;
  175.         }
  176.         else if (iconString.equals("cloudy")) {
  177.             iconId = R.drawable.cloudy;
  178.         }
  179.         else if (iconString.equals("partly-cloudy-day")) {
  180.             iconId = R.drawable.partly_cloudy;
  181.         }
  182.         else if (iconString.equals("partly-cloudy-night")) {
  183.             iconId = R.drawable.cloudy_night;
  184.         }
  185.  
  186.         return iconId;
  187. */
  188.     }
  189.  
  190.     public void setIcon(String icon) {
  191.         mIcon = icon;
  192.     }
  193.  
  194.     public int getPrecipChance() {
  195.         double precipPercentage = mPrecipChance * 100;
  196.         return (int)Math.round(precipPercentage);
  197.     }
  198.  
  199.     public void setPrecipChance(double precipChance) {
  200.         mPrecipChance = precipChance;
  201.     }
  202.  
  203.     public String getSummary() {
  204.         return mSummary;
  205.     }
  206.  
  207.     public void setSummary(String summary) {
  208.         mSummary = summary;
  209.     }
  210.  
  211.     public int getTemperature() {
  212.         return (int)Math.round(mTemperature);
  213.     }
  214.  
  215.     public void setTemperature(double temperature) {
  216.         mTemperature = temperature;
  217.     }
  218.  
  219.     public long getTime() {
  220.         return mTime;
  221.     }
  222.  
  223.     public String getFormattedTime() {
  224.         SimpleDateFormat formatter = new SimpleDateFormat("h:mm, a");
  225.         formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
  226.         Date dateTime = new Date(getTime() * 1000);
  227.         String timeString = formatter.format(dateTime);
  228.  
  229.         return timeString;
  230.     }
  231.  
  232.     public void setTime(long time) {
  233.         mTime = time;
  234.     }
  235.  
  236. }
  237.  
  238.  
  239. //-----------------------------------------------------------
  240.  
  241. DayAdapter.java
  242.  
  243. package com.teamtreehouse.stormy.adapters;
  244.  
  245. import android.content.Context;
  246. import android.view.LayoutInflater;
  247. import android.view.View;
  248. import android.view.ViewGroup;
  249. import android.widget.BaseAdapter;
  250. import android.widget.ImageView;
  251. import android.widget.TextView;
  252.  
  253. import com.teamtreehouse.stormy.R;
  254. import com.teamtreehouse.stormy.weather.Day;
  255.  
  256. /**
  257.  * Created by Fu on 7/16/2015.
  258.  */
  259. public class DayAdapter extends BaseAdapter {
  260.  
  261.  
  262.     private Context mContext;
  263.     private Day[] mDays;
  264.  
  265.     public DayAdapter(Context context, Day[] days){
  266.         mContext = context;
  267.         mDays = days;
  268.     }
  269.  
  270.  
  271.     @Override
  272.     public int getCount() {
  273.         return mDays.length;
  274.     }
  275.  
  276.     @Override
  277.     public Object getItem(int position) {
  278.         return mDays[position];
  279.     }
  280.  
  281.     @Override
  282.     public long getItemId(int position) {
  283.         return 0;
  284.     }
  285.  
  286.     @Override
  287.     public View getView(int position, View convertView, ViewGroup parent) { //positon parameter (int postion)*
  288.         ViewHolder holder;
  289.  
  290.         if (convertView == null) {
  291.             convertView = LayoutInflater.from(mContext).inflate(R.layout.daily_list_item, null);
  292.  
  293.             holder = new ViewHolder();
  294.             holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
  295.             holder.temperatureLabel = (TextView) convertView.findViewById(R.id.temperatureLabel);
  296.             holder.dayLabel = (TextView) convertView.findViewById(R.id.dayNameLabel);
  297.  
  298.             convertView.setTag(holder);
  299.         }
  300.         else {
  301.                 holder = (ViewHolder) convertView.getTag();
  302.         }
  303.         Day day = mDays[position]; //*[from the position parameter]
  304.         holder.iconImageView.setImageResource(day.getIconId());
  305.         holder.temperatureLabel.setText(day.getTemperatureMax() + ""); //getTemperatureMax() would get a Double value thus we convert it to a String with + ""
  306.         holder.dayLabel.setText(day.getDayOfTheWeek());
  307.  
  308.         return convertView;
  309.     }
  310.  
  311.     private static class ViewHolder {
  312.         ImageView iconImageView;
  313.         TextView temperatureLabel;
  314.         TextView dayLabel;
  315.     }
  316. }
  317.  
  318.  
  319. //---------------------------------------------------------------
  320.  
  321. Day.java
  322.  
  323. package com.teamtreehouse.stormy.weather;
  324.  
  325. import java.text.SimpleDateFormat;
  326. import java.util.Date;
  327. import java.util.TimeZone;
  328.  
  329. public class Day {
  330.     private long mTime;
  331.     private String mSummary;
  332.     private double mTemperatureMax; //max temp of the day
  333.     private String mIcon;
  334.     private String mTimeZone;
  335.  
  336.  
  337.     public String getIcon() {
  338.         return mIcon;
  339.     }
  340.  
  341.     public void setIcon(String icon) {
  342.         mIcon = icon;
  343.     }
  344.  
  345.     public String getSummary() {
  346.         return mSummary;
  347.     }
  348.  
  349.     public void setSummary(String summary) {
  350.         mSummary = summary;
  351.     }
  352.  
  353.     public int getTemperatureMax() { //convert from "double" to an "int"
  354.         return (int) Math.round(mTemperatureMax);
  355.     }
  356.  
  357.     public void setTemperatureMax(double temperatureMax) {
  358.         mTemperatureMax = temperatureMax;
  359.     }
  360.  
  361.     public long getTime() {
  362.         return mTime;
  363.     }
  364.  
  365.     public void setTime(long time) {
  366.         mTime = time;
  367.     }
  368.  
  369.     public String getTimezone() {
  370.         return mTimeZone;
  371.     }
  372.  
  373.     public void setTimezone(String timeZone) {
  374.         mTimeZone = timeZone;
  375.     }
  376.  
  377.     public int getIconId(){
  378.         return Forecast.getIconId(mIcon);
  379.     }
  380.  
  381.     //Create Day Of The Week method
  382.     public String getDayOfTheWeek(){
  383.         SimpleDateFormat formatter = new SimpleDateFormat("EEEE"); //use SimpleDateFormat just like before in h:mm, a //And the format we want to use here for the Day value from the long timestamp is just four capital Es, EEEE.
  384.         formatter.setTimeZone(TimeZone.getTimeZone(mTimezone)); //this is why we created the TimeZone class. Get the Timezone method from that class and pass in the String mTimeZone --> So this takes the string that we get from the Forecast API and its a standard format that can be converted into a timezone object to use here.
  385.         Date dateTime = new Date(mTime * 1000); //Date constructor expects number in seconds but mTime outputs in miliseconds so * 1000 to convert it.
  386.         return formatter.format(dateTime);
  387.  
  388.  
  389.     }
  390.  
  391.  
  392. //---------------------------------------------------------------
  393.  
  394. DailyForecastActivity.java
  395.  
  396. package com.teamtreehouse.stormy.UI;
  397.  
  398. import android.app.ListActivity;
  399. import android.support.v7.app.ActionBarActivity;
  400. import android.os.Bundle;
  401. import android.view.Menu;
  402. import android.view.MenuItem;
  403. import android.view.View;
  404. import android.widget.ArrayAdapter;
  405.  
  406. import com.teamtreehouse.stormy.R;
  407. import com.teamtreehouse.stormy.adapters.DayAdapter;
  408. import com.teamtreehouse.stormy.weather.Day;
  409.  
  410. public class DailyForecastActivity extends ListActivity { //change 'ActionBarActivity' to 'ListActivity'
  411.  
  412.     private Day[] mDays;
  413.    
  414.     @Override
  415.     protected void onCreate(Bundle savedInstanceState) {
  416.         super.onCreate(savedInstanceState);
  417.         setContentView(R.layout.activity_daily_forecast);
  418.  
  419.        // Deleted default adapter
  420.        // Create a new DayAdapter
  421.         DayAdapter adapter = new DayAdapter(this, mDays);
  422.  
  423.     }
  424. }
  425.  
  426.  
  427. // Done with Custom List Views seciton!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement