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

Commit c6432e8

Browse files
author
Lyla
committed
5.10 Coding Task on Selected Item on Tablet
1 parent 1ea4116 commit c6432e8

File tree

6 files changed

+100
-38
lines changed

6 files changed

+100
-38
lines changed

app/src/main/java/com/example/android/sunshine/app/DetailActivity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@ public class DetailActivity extends ActionBarActivity {
2828
protected void onCreate(Bundle savedInstanceState) {
2929
super.onCreate(savedInstanceState);
3030
setContentView(R.layout.activity_detail);
31+
3132
if (savedInstanceState == null) {
33+
// Create the detail fragment and add it to the activity
34+
// using a fragment transaction.
35+
36+
Bundle arguments = new Bundle();
37+
arguments.putParcelable(DetailFragment.DETAIL_URI, getIntent().getData());
38+
39+
DetailFragment fragment = new DetailFragment();
40+
fragment.setArguments(arguments);
41+
3242
getSupportFragmentManager().beginTransaction()
33-
.add(R.id.weather_detail_container, new DetailFragment())
43+
.add(R.id.weather_detail_container, fragment)
3444
.commit();
3545
}
3646
}

app/src/main/java/com/example/android/sunshine/app/DetailFragment.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
import android.content.Intent;
1919
import android.database.Cursor;
20+
import android.net.Uri;
2021
import android.os.Bundle;
2122
import android.support.v4.app.Fragment;
2223
import android.support.v4.app.LoaderManager;
2324
import android.support.v4.content.CursorLoader;
2425
import android.support.v4.content.Loader;
2526
import android.support.v4.view.MenuItemCompat;
2627
import android.support.v7.widget.ShareActionProvider;
27-
import android.util.Log;
2828
import android.view.LayoutInflater;
2929
import android.view.Menu;
3030
import android.view.MenuInflater;
@@ -43,11 +43,13 @@
4343
public class DetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
4444

4545
private static final String LOG_TAG = DetailFragment.class.getSimpleName();
46+
static final String DETAIL_URI = "URI";
4647

4748
private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp";
4849

4950
private ShareActionProvider mShareActionProvider;
5051
private String mForecast;
52+
private Uri mUri;
5153

5254
private static final int DETAIL_LOADER = 0;
5355

@@ -97,6 +99,12 @@ public DetailFragment() {
9799
@Override
98100
public View onCreateView(LayoutInflater inflater, ViewGroup container,
99101
Bundle savedInstanceState) {
102+
103+
Bundle arguments = getArguments();
104+
if (arguments != null) {
105+
mUri = arguments.getParcelable(DetailFragment.DETAIL_URI);
106+
}
107+
100108
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
101109
mIconView = (ImageView) rootView.findViewById(R.id.detail_icon);
102110
mDateView = (TextView) rootView.findViewById(R.id.detail_date_textview);
@@ -141,24 +149,32 @@ public void onActivityCreated(Bundle savedInstanceState) {
141149
super.onActivityCreated(savedInstanceState);
142150
}
143151

152+
void onLocationChanged( String newLocation ) {
153+
// replace the uri, since the location has changed
154+
Uri uri = mUri;
155+
if (null != uri) {
156+
long date = WeatherContract.WeatherEntry.getDateFromUri(uri);
157+
Uri updatedUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(newLocation, date);
158+
mUri = updatedUri;
159+
getLoaderManager().restartLoader(DETAIL_LOADER, null, this);
160+
}
161+
}
162+
144163
@Override
145164
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
146-
Log.v(LOG_TAG, "In onCreateLoader");
147-
Intent intent = getActivity().getIntent();
148-
if (intent == null || intent.getData() == null) {
149-
return null;
165+
if ( null != mUri ) {
166+
// Now create and return a CursorLoader that will take care of
167+
// creating a Cursor for the data being displayed.
168+
return new CursorLoader(
169+
getActivity(),
170+
mUri,
171+
DETAIL_COLUMNS,
172+
null,
173+
null,
174+
null
175+
);
150176
}
151-
152-
// Now create and return a CursorLoader that will take care of
153-
// creating a Cursor for the data being displayed.
154-
return new CursorLoader(
155-
getActivity(),
156-
intent.getData(),
157-
DETAIL_COLUMNS,
158-
null,
159-
null,
160-
null
161-
);
177+
return null;
162178
}
163179

164180
@Override

app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.example.android.sunshine.app;
1717

18-
import android.content.Intent;
1918
import android.database.Cursor;
2019
import android.net.Uri;
2120
import android.os.Bundle;
@@ -74,6 +73,18 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa
7473

7574
private ForecastAdapter mForecastAdapter;
7675

76+
/**
77+
* A callback interface that all activities containing this fragment must
78+
* implement. This mechanism allows activities to be notified of item
79+
* selections.
80+
*/
81+
public interface Callback {
82+
/**
83+
* DetailFragmentCallback for when an item has been selected.
84+
*/
85+
public void onItemSelected(Uri dateUri);
86+
}
87+
7788
public ForecastFragment() {
7889
}
7990

@@ -124,11 +135,10 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
124135
Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
125136
if (cursor != null) {
126137
String locationSetting = Utility.getPreferredLocation(getActivity());
127-
Intent intent = new Intent(getActivity(), DetailActivity.class)
128-
.setData(WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
138+
((Callback) getActivity())
139+
.onItemSelected(WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
129140
locationSetting, cursor.getLong(COL_WEATHER_DATE)
130141
));
131-
startActivity(intent);
132142
}
133143
}
134144
});

app/src/main/java/com/example/android/sunshine/app/MainActivity.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import android.view.Menu;
2424
import android.view.MenuItem;
2525

26-
public class MainActivity extends ActionBarActivity {
26+
public class MainActivity extends ActionBarActivity implements ForecastFragment.Callback {
2727

2828
private final String LOG_TAG = MainActivity.class.getSimpleName();
2929
private static final String DETAILFRAGMENT_TAG = "DFTAG";
@@ -112,7 +112,33 @@ protected void onResume() {
112112
if ( null != ff ) {
113113
ff.onLocationChanged();
114114
}
115+
DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentByTag(DETAILFRAGMENT_TAG);
116+
if ( null != df ) {
117+
df.onLocationChanged(location);
118+
}
115119
mLocation = location;
116120
}
117121
}
122+
123+
@Override
124+
public void onItemSelected(Uri contentUri) {
125+
if (mTwoPane) {
126+
// In two-pane mode, show the detail view in this activity by
127+
// adding or replacing the detail fragment using a
128+
// fragment transaction.
129+
Bundle args = new Bundle();
130+
args.putParcelable(DetailFragment.DETAIL_URI, contentUri);
131+
132+
DetailFragment fragment = new DetailFragment();
133+
fragment.setArguments(args);
134+
135+
getSupportFragmentManager().beginTransaction()
136+
.replace(R.id.weather_detail_container, fragment, DETAILFRAGMENT_TAG)
137+
.commit();
138+
} else {
139+
Intent intent = new Intent(this, DetailActivity.class)
140+
.setData(contentUri);
141+
startActivity(intent);
142+
}
143+
}
118144
}

app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.example.android.sunshine.app;
1717

18+
import android.annotation.TargetApi;
19+
import android.content.Intent;
20+
import android.os.Build;
1821
import android.os.Bundle;
1922
import android.preference.ListPreference;
2023
import android.preference.Preference;
@@ -80,4 +83,9 @@ public boolean onPreferenceChange(Preference preference, Object value) {
8083
return true;
8184
}
8285

86+
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
87+
@Override
88+
public Intent getParentActivityIntent() {
89+
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
90+
}
8391
}

app/src/main/res/layout/fragment_detail.xml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
<TextView
1414
android:id="@+id/detail_day_textview"
1515
android:layout_width="wrap_content"
16-
android:layout_height="wrap_content"
17-
android:text="Tomorrow" />
16+
android:layout_height="wrap_content" />
1817

1918
<TextView
2019
android:id="@+id/detail_date_textview"
2120
android:layout_width="wrap_content"
22-
android:layout_height="wrap_content"
23-
android:text="June 24"/>
21+
android:layout_height="wrap_content" />
2422

2523
<!-- Main content: high, low, art, weather state -->
2624
<LinearLayout
@@ -38,14 +36,12 @@
3836
<TextView
3937
android:id="@+id/detail_high_textview"
4038
android:layout_width="wrap_content"
41-
android:layout_height="wrap_content"
42-
android:text="21°"/>
39+
android:layout_height="wrap_content" />
4340

4441
<TextView
4542
android:id="@+id/detail_low_textview"
4643
android:layout_width="wrap_content"
47-
android:layout_height="wrap_content"
48-
android:text="11°" />
44+
android:layout_height="wrap_content" />
4945
</LinearLayout>
5046

5147
<LinearLayout
@@ -63,28 +59,24 @@
6359
<TextView
6460
android:id="@+id/detail_forecast_textview"
6561
android:layout_width="wrap_content"
66-
android:layout_height="wrap_content"
67-
android:text="Clear" />
62+
android:layout_height="wrap_content" />
6863
</LinearLayout>
6964
</LinearLayout>
7065

7166
<!-- Humidity, wind, pressure -->
7267
<TextView
7368
android:id="@+id/detail_humidity_textview"
7469
android:layout_width="match_parent"
75-
android:layout_height="wrap_content"
76-
android:text="48%" />
70+
android:layout_height="wrap_content" />
7771

7872
<TextView
7973
android:id="@+id/detail_wind_textview"
8074
android:layout_width="match_parent"
81-
android:layout_height="wrap_content"
82-
android:text="6 km/h NW" />
75+
android:layout_height="wrap_content" />
8376

8477
<TextView
8578
android:id="@+id/detail_pressure_textview"
8679
android:layout_width="match_parent"
87-
android:layout_height="wrap_content"
88-
android:text="1014" />
80+
android:layout_height="wrap_content" />
8981
</LinearLayout>
9082
</ScrollView>

0 commit comments

Comments
 (0)