1 package uk
.me
.njae
.sunshine
;
3 import android
.content
.Intent
;
4 import android
.content
.SharedPreferences
;
5 import android
.net
.Uri
;
6 import android
.os
.Bundle
;
7 import android
.preference
.PreferenceManager
;
8 import android
.support
.v4
.app
.Fragment
;
9 import android
.util
.Log
;
10 import android
.view
.LayoutInflater
;
11 import android
.view
.Menu
;
12 import android
.view
.MenuInflater
;
13 import android
.view
.MenuItem
;
14 import android
.view
.View
;
15 import android
.view
.ViewGroup
;
16 import android
.widget
.AdapterView
;
17 import android
.widget
.ArrayAdapter
;
18 import android
.widget
.ListView
;
20 import java
.io
.UnsupportedEncodingException
;
21 import java
.net
.URLEncoder
;
22 import java
.util
.ArrayList
;
25 * A placeholder fragment containing a simple view.
27 public class ForecastFragment
extends Fragment
{
29 private final String LOG_TAG
= ForecastFragment
.class.getSimpleName();
31 private ArrayAdapter
<String
> mForecastAdapter
;
33 public ForecastFragment() {
37 public void onCreate(Bundle savedInstanceState
) {
38 super.onCreate(savedInstanceState
);
39 setHasOptionsMenu(true);
43 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
44 // Inflate the menu; this adds items to the action bar if it is present.
45 inflater
.inflate(R
.menu
.forecastfragment
, menu
);
49 public boolean onOptionsItemSelected(MenuItem item
) {
50 // Handle action bar item clicks here. The action bar will
51 // automatically handle clicks on the Home/Up button, so long
52 // as you specify a parent activity in AndroidManifest.xml.
53 int id
= item
.getItemId();
54 if (id
== R
.id
.action_refresh
) {
58 if (id
== R
.id
.action_show_location
) {
59 SharedPreferences preferences
= PreferenceManager
.getDefaultSharedPreferences(getActivity());
60 String location
= preferences
.getString(getString(R
.string
.pref_location_key
),
61 getString(R
.string
.pref_location_default
));
62 Intent intent
= new Intent(Intent
.ACTION_VIEW
);
65 geoLocation
= Uri
.parse("geo:0,0?q=" + URLEncoder
.encode(location
, "UTF-8"));
66 intent
.setData(geoLocation
);
67 } catch (UnsupportedEncodingException e
) {
68 Log
.e(LOG_TAG
, "Error ", e
);
70 if (intent
.resolveActivity(getActivity().getPackageManager()) != null) {
71 startActivity(intent
);
75 return super.onOptionsItemSelected(item
);
78 private void updateWeather() {
79 String location
= Utility
.getPreferredLocation(getActivity());
80 new FetchWeatherTask(getActivity(), mForecastAdapter
).execute(location
);
84 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
85 Bundle savedInstanceState
) {
87 mForecastAdapter
= new ArrayAdapter
<String
>(
89 R
.layout
.list_item_forecast
,
90 R
.id
.list_item_forecast_textview
,
91 new ArrayList
<String
>() // weekForecast
94 View rootView
= inflater
.inflate(R
.layout
.fragment_main
, container
, false);
96 ListView listView
= (ListView
) rootView
.findViewById(R
.id
.listview_forecast
);
97 listView
.setAdapter(mForecastAdapter
);
98 listView
.setOnItemClickListener(new AdapterView
.OnItemClickListener() {
101 public void onItemClick(AdapterView
<?
> adapterView
, View view
, int position
, long l
) {
102 String forecast
= mForecastAdapter
.getItem(position
);
103 Intent intent
= new Intent(getActivity(), DetailActivity
.class)
104 .putExtra(Intent
.EXTRA_TEXT
, forecast
);
105 startActivity(intent
);
112 public void onStart() {