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

Commit 0d060ff

Browse files
author
Lyla
committed
3.04 Get the forecast from the intent and use it to populate the detail text
1 parent 4a61c5a commit 0d060ff

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package com.example.android.sunshine.app;
1818

1919
import android.support.v7.app.ActionBarActivity;
20-
import android.support.v4.app.Fragment;
20+
import android.content.Intent;
2121
import android.os.Bundle;
22+
import android.support.v4.app.Fragment;
2223
import android.view.LayoutInflater;
2324
import android.view.Menu;
2425
import android.view.MenuItem;
2526
import android.view.View;
2627
import android.view.ViewGroup;
28+
import android.widget.TextView;
2729

2830
public class DetailActivity extends ActionBarActivity {
2931

@@ -33,7 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
3335
setContentView(R.layout.activity_detail);
3436
if (savedInstanceState == null) {
3537
getSupportFragmentManager().beginTransaction()
36-
.add(R.id.container, new PlaceholderFragment())
38+
.add(R.id.container, new DetailFragment())
3739
.commit();
3840
}
3941
}
@@ -64,16 +66,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
6466
/**
6567
* A placeholder fragment containing a simple view.
6668
*/
67-
public static class PlaceholderFragment extends Fragment {
69+
public static class DetailFragment extends Fragment {
6870

69-
public PlaceholderFragment() {
71+
public DetailFragment() {
7072
}
7173

7274
@Override
7375
public View onCreateView(LayoutInflater inflater, ViewGroup container,
7476
Bundle savedInstanceState) {
7577

7678
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
79+
80+
// The detail Activity called via intent. Inspect the intent for forecast data.
81+
Intent intent = getActivity().getIntent();
82+
if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
83+
String forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
84+
((TextView) rootView.findViewById(R.id.detail_text))
85+
.setText(forecastStr);
86+
}
87+
7788
return rootView;
7889
}
7990
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
android:paddingRight="@dimen/activity_horizontal_margin"
55
android:paddingTop="@dimen/activity_vertical_margin"
66
android:paddingBottom="@dimen/activity_vertical_margin"
7-
tools:context="com.example.android.sunshine.app.DetailActivity$PlaceholderFragment">
7+
tools:context="com.example.android.sunshine.app.DetailActivity.DetailFragment">
88

99
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
10+
android:id="@+id/detail_text"
1011
android:layout_height="wrap_content" />
1112

1213
</RelativeLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<!-- Menu label to fetch updated weather info from the server -->
1818
<string name="action_refresh" translatable="false">Refresh</string>
19+
<string name="title_activity_detail">Details</string>
1920
<string name="hello_world">Hello world!</string>
20-
<string name="title_activity_detail">DetailActivity</string>
2121

2222
</resources>

0 commit comments

Comments
 (0)