22
33import android .app .*;
44import android .content .Context ;
5+ import android .content .Intent ;
56import android .os .Bundle ;
67import android .support .v13 .app .FragmentPagerAdapter ;
78import android .support .v4 .view .ViewPager ;
9+ import android .util .Log ;
810import android .view .*;
11+ import android .widget .AdapterView ;
912import android .widget .GridView ;
1013
1114public class MainActivity extends Activity implements ActionBar .TabListener {
@@ -25,18 +28,20 @@ public class MainActivity extends Activity implements ActionBar.TabListener {
2528 */
2629 ViewPager mViewPager ;
2730
28- private static final String TAG = "r3ader" ;
31+
32+ //ViewPager.setOffscreenPageLimit(int) way to control how many pages
33+
34+ private static final String TAG = MainActivity .class .getName ();
35+ private String [] sections ;
2936
3037 @ Override
3138 protected void onCreate (Bundle savedInstanceState ) {
3239 super .onCreate (savedInstanceState );
3340 setContentView (R .layout .activity_main );
34-
35- // Set up the action bar.
41+ sections = getResources ().getStringArray (R .array .sections );
3642 final ActionBar actionBar = getActionBar ();
3743 actionBar .setNavigationMode (ActionBar .NAVIGATION_MODE_TABS );
3844
39- int sections = 4 ;
4045 // Create the adapter that will return a fragment for each of the three
4146 // primary sections of the activity.
4247 mSectionsPagerAdapter = new SectionsPagerAdapter (getFragmentManager (), this , sections );
@@ -54,13 +59,20 @@ public void onPageSelected(int position) {
5459 actionBar .setSelectedNavigationItem (position );
5560 }
5661 });
57- String [] sectionNames = {"In Theaters" , "DVD Releases" , "Opening" , "Box Office" };
58- for (int i = 0 ; i < sections ; i ++) {
59- actionBar .addTab (actionBar .newTab ().setText (sectionNames [i ]).setTabListener (this ));
62+ for (String section : sections ) {
63+ actionBar .addTab (actionBar .newTab ().setText (getName (section )).setTabListener (this ));
6064 }
6165 }
6266
6367
68+ /* String name in resource files can not have spaces,
69+ hence replace the _ with spaces to get the actual desired
70+ values.
71+ */
72+ public String getName (String withUnderscores ) {
73+ return withUnderscores .replaceAll ("_" , " " );
74+ }
75+
6476 @ Override
6577 public boolean onCreateOptionsMenu (Menu menu ) {
6678
@@ -103,15 +115,9 @@ public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTrans
103115 public class SectionsPagerAdapter extends FragmentPagerAdapter {
104116
105117 private Context context ;
106- private int sections ;
107- private String [] LIST = {
108- "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=24&page=1&country=us&apikey=" ,
109- "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?page_limit=24&page=1&country=us&apikey=" ,
110- "http://api.rottentomatoes.com/api/public/v1.0/lists/dvd/upcoming.json?page_limit=24&page=1&country=us&apikey=" ,
111- "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?page_limit=24&page=1&country=us&apikey="
112- };
113-
114- public SectionsPagerAdapter (FragmentManager fm , Context context , int sections ) {
118+ private String [] sections ;
119+
120+ public SectionsPagerAdapter (FragmentManager fm , Context context , String [] sections ) {
115121 super (fm );
116122 this .context = context ;
117123 this .sections = sections ;
@@ -121,54 +127,74 @@ public SectionsPagerAdapter(FragmentManager fm, Context context, int sections) {
121127 public Fragment getItem (int position ) {
122128 // getItem is called to instantiate the fragment for the given page.
123129 // Return a PlaceholderFragment (defined as a static inner class below).
124- GridAdapter gridAdapter = new GridAdapter (this .context , LIST [position ]);
125- return PlaceholderFragment .newInstance (position , context , gridAdapter );
130+ return PlaceholderFragment .newInstance (position , context );
126131 }
127132
128133 @ Override
129134 public int getCount () {
130135 // Show 3 total pages.
131- return sections ;
136+ return sections . length ;
132137 }
133138 }
134139
135140 /**
136141 * A placeholder fragment containing a simple view.
137142 */
138143 public static class PlaceholderFragment extends Fragment {
139- /**
140- * The fragment argument representing the section number for this
141- * fragment.
142- */
143- private static final String ARG_SECTION_NUMBER = "section_number" ;
144-
145144 private int position ;
146- private GridAdapter gridAdapter ;
145+ private static String [] sections ;
147146
148147 /**
149148 * Returns a new instance of this fragment for the given section
150149 * number.
151150 */
152- public static PlaceholderFragment newInstance (int sectionNumber , Context context , GridAdapter gridAdapter ) {
151+ public static PlaceholderFragment newInstance (int sectionNumber , Context context ) {
153152 PlaceholderFragment fragment = new PlaceholderFragment ();
154153 Bundle args = new Bundle ();
155- args .putInt (ARG_SECTION_NUMBER , sectionNumber );
156- fragment .position = sectionNumber ;
154+ args .putInt ("position" , sectionNumber );
157155 fragment .setArguments (args );
158- fragment .gridAdapter = gridAdapter ;
156+ fragment .sections = context .getResources ().getStringArray (R .array .sections );
157+ fragment .position = sectionNumber ;
158+ Log .d (TAG , "creating a new fragment" );
159159 return fragment ;
160160 }
161161
162- public PlaceholderFragment () {
163- }
164-
165162 @ Override
166163 public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
164+ int labelId = getResources ().getIdentifier (sections [position ], "string" , getActivity ().getPackageName ());
165+ String url = getResources ().getString (labelId );
166+ String KEY = getString (R .string .ROTTEN_KEY );
167+ GridAdapter gridAdapter = new GridAdapter (this .getActivity (), url + KEY );
168+
169+
167170 View rootView = inflater .inflate (R .layout .fragment_main , container , false );
168171 GridView gridview = (GridView ) rootView .findViewById (R .id .gridView );
172+ Log .w (TAG ,gridAdapter .toString ());
169173 gridview .setAdapter (gridAdapter );
174+ gridview .setOnItemClickListener ((AdapterView <?> parent , View v , int position , long id ) -> {
175+
176+ Movie movie = gridAdapter .getItem (position );
177+ Intent intent = new Intent (this .getActivity (), MovieActivity .class );
178+ intent .putExtra ("movie" , movie );
179+ this .getActivity ().startActivity (intent );
180+ });
181+ Log .d (TAG , "creating new grid" );
170182 return rootView ;
171183 }
184+
185+ @ Override
186+ public void onCreate (Bundle savedInstanceState ) {
187+ super .onCreate (savedInstanceState );
188+ if (getArguments () != null ) {
189+ position = getArguments ().getInt ("position" );
190+ }
191+ }
192+
193+ @ Override
194+ public void onDestroy () {
195+ super .onDestroy ();
196+ Log .w (TAG ,"Destroying" );
197+ }
172198 }
173199
174200}
0 commit comments