Advertisement
anta40

MainActivtity.java

Apr 21st, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.34 KB | None | 0 0
  1. package com.anta40.reksawatch;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9.  
  10. import com.android.volley.Response;
  11. import com.android.volley.VolleyError;
  12. import com.android.volley.toolbox.JsonArrayRequest;
  13. import com.anta40.reksawatch.adapter.CustomListAdapter;
  14. import com.anta40.reksawatch.app.AppController;
  15. import com.anta40.reksawatch.model.Movie;
  16.  
  17. import android.app.Activity;
  18. import android.app.ActionBar;
  19. import android.app.Fragment;
  20. import android.app.FragmentManager;
  21. import android.app.ProgressDialog;
  22. import android.content.Context;
  23. import android.graphics.Color;
  24. import android.graphics.drawable.ColorDrawable;
  25. import android.os.Build;
  26. import android.os.Bundle;
  27. import android.util.Log;
  28. import android.view.Gravity;
  29. import android.view.LayoutInflater;
  30. import android.view.Menu;
  31. import android.view.MenuItem;
  32. import android.view.View;
  33. import android.view.ViewGroup;
  34. import android.support.v4.widget.DrawerLayout;
  35. import android.widget.ArrayAdapter;
  36. import android.widget.ListView;
  37. import android.widget.TextView;
  38.  
  39. public class MainActivity extends Activity implements
  40.         NavigationDrawerFragment.NavigationDrawerCallbacks {
  41.  
  42.     // Log tag
  43.     private static final String TAG = MainActivity.class.getSimpleName();
  44.  
  45.     // Movies json url
  46.     private static final String url = "http://api.androidhive.info/json/movies.json";
  47.     private ProgressDialog pDialog;
  48.     private List<Movie> movieList = new ArrayList<Movie>();
  49.     private ListView listView;
  50.     private CustomListAdapter adapter;
  51.    
  52.     /**
  53.      * Fragment managing the behaviors, interactions and presentation of the
  54.      * navigation drawer.
  55.      */
  56.     private NavigationDrawerFragment mNavigationDrawerFragment;
  57.  
  58.     /**
  59.      * Used to store the last screen title. For use in
  60.      * {@link #restoreActionBar()}.
  61.      */
  62.     private CharSequence mTitle;
  63.  
  64.     @Override
  65.     protected void onCreate(Bundle savedInstanceState) {
  66.         super.onCreate(savedInstanceState);
  67.         setContentView(R.layout.activity_main);
  68.  
  69.         mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
  70.                 .findFragmentById(R.id.navigation_drawer);
  71.         mTitle = getTitle();
  72.  
  73.         // Set up the drawer.
  74.         mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
  75.                 (DrawerLayout) findViewById(R.id.drawer_layout));
  76.        
  77.          super.onCreate(savedInstanceState);
  78.             setContentView(R.layout.activity_main);
  79.      
  80.             listView = (ListView) findViewById(R.id.list);
  81.             adapter = new CustomListAdapter(this, movieList);
  82.             listView.setAdapter(adapter);
  83.      
  84.             pDialog = new ProgressDialog(this);
  85.             // Showing progress dialog before making http request
  86.             pDialog.setMessage("Loading...");
  87.             pDialog.show();
  88.      
  89.             // changing action bar color
  90.             getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));
  91.      
  92.             // Creating volley request obj
  93.             JsonArrayRequest movieReq = new JsonArrayRequest(url,
  94.                     new Response.Listener<JSONArray>() {
  95.                         @Override
  96.                         public void onResponse(JSONArray response) {
  97.                             Log.d(TAG, response.toString());
  98.                             hidePDialog();
  99.      
  100.                             // Parsing json
  101.                             for (int i = 0; i < response.length(); i++) {
  102.                                 try {
  103.      
  104.                                     JSONObject obj = response.getJSONObject(i);
  105.                                     Movie movie = new Movie();
  106.                                     movie.setTitle(obj.getString("title"));
  107.                                     movie.setThumbnailUrl(obj.getString("image"));
  108.                                     movie.setRating(((Number) obj.get("rating"))
  109.                                             .doubleValue());
  110.                                     movie.setYear(obj.getInt("releaseYear"));
  111.      
  112.                                     // Genre is json array
  113.                                     JSONArray genreArry = obj.getJSONArray("genre");
  114.                                     ArrayList<String> genre = new ArrayList<String>();
  115.                                     for (int j = 0; j < genreArry.length(); j++) {
  116.                                         genre.add((String) genreArry.get(j));
  117.                                     }
  118.                                     movie.setGenre(genre);
  119.      
  120.                                     // adding movie to movies array
  121.                                     movieList.add(movie);
  122.      
  123.                                 } catch (JSONException e) {
  124.                                     e.printStackTrace();
  125.                                 }
  126.      
  127.                             }
  128.      
  129.                             // notifying list adapter about data changes
  130.                             // so that it renders the list view with updated data
  131.                             adapter.notifyDataSetChanged();
  132.                         }
  133.                     }, new Response.ErrorListener() {
  134.                         @Override
  135.                         public void onErrorResponse(VolleyError error) {
  136.                          //   VolleyLog.d(TAG, "Error: " + error.getMessage());
  137.                             hidePDialog();
  138.      
  139.                         }
  140.                     });
  141.      
  142.             // Adding request to request queue
  143.             AppController.getInstance().addToRequestQueue(movieReq);
  144.     }
  145.  
  146.     @Override
  147.     public void onDestroy() {
  148.         super.onDestroy();
  149.         hidePDialog();
  150.     }
  151.  
  152.     private void hidePDialog() {
  153.         if (pDialog != null) {
  154.             pDialog.dismiss();
  155.             pDialog = null;
  156.         }
  157.     }
  158.    
  159.     @Override
  160.     public void onNavigationDrawerItemSelected(int position) {
  161.         // update the main content by replacing fragments
  162.         FragmentManager fragmentManager = getFragmentManager();
  163.         fragmentManager
  164.                 .beginTransaction()
  165.                 .replace(R.id.container,
  166.                         PlaceholderFragment.newInstance(position + 1)).commit();
  167.     }
  168.  
  169.     public void onSectionAttached(int number) {
  170.         switch (number) {
  171.         case 1:
  172.             mTitle = getString(R.string.title_section1);
  173.             break;
  174.         case 2:
  175.             mTitle = getString(R.string.title_section2);
  176.             break;
  177.         case 3:
  178.             mTitle = getString(R.string.title_section3);
  179.             break;
  180.         }
  181.     }
  182.  
  183.     public void restoreActionBar() {
  184.         ActionBar actionBar = getActionBar();
  185.         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  186.         actionBar.setDisplayShowTitleEnabled(true);
  187.         actionBar.setTitle(mTitle);
  188.     }
  189.  
  190.     @Override
  191.     public boolean onCreateOptionsMenu(Menu menu) {
  192.         if (!mNavigationDrawerFragment.isDrawerOpen()) {
  193.             // Only show items in the action bar relevant to this screen
  194.             // if the drawer is not showing. Otherwise, let the drawer
  195.             // decide what to show in the action bar.
  196.             getMenuInflater().inflate(R.menu.main, menu);
  197.             restoreActionBar();
  198.             return true;
  199.         }
  200.         return super.onCreateOptionsMenu(menu);
  201.     }
  202.  
  203.     @Override
  204.     public boolean onOptionsItemSelected(MenuItem item) {
  205.         // Handle action bar item clicks here. The action bar will
  206.         // automatically handle clicks on the Home/Up button, so long
  207.         // as you specify a parent activity in AndroidManifest.xml.
  208.         int id = item.getItemId();
  209.         if (id == R.id.action_settings) {
  210.             return true;
  211.         }
  212.         return super.onOptionsItemSelected(item);
  213.     }
  214.  
  215.     /**
  216.      * A placeholder fragment containing a simple view.
  217.      */
  218.     public static class PlaceholderFragment extends Fragment {
  219.         /**
  220.          * The fragment argument representing the section number for this
  221.          * fragment.
  222.          */
  223.         private static final String ARG_SECTION_NUMBER = "section_number";
  224.  
  225.         /**
  226.          * Returns a new instance of this fragment for the given section number.
  227.          */
  228.         public static PlaceholderFragment newInstance(int sectionNumber) {
  229.             PlaceholderFragment fragment = new PlaceholderFragment();
  230.             Bundle args = new Bundle();
  231.             args.putInt(ARG_SECTION_NUMBER, sectionNumber);
  232.             fragment.setArguments(args);
  233.             return fragment;
  234.         }
  235.  
  236.         public PlaceholderFragment() {
  237.         }
  238.  
  239.         @Override
  240.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  241.                 Bundle savedInstanceState) {
  242.             View rootView = inflater.inflate(R.layout.fragment_main, container,
  243.                     false);
  244.             return rootView;
  245.         }
  246.  
  247.         @Override
  248.         public void onAttach(Activity activity) {
  249.             super.onAttach(activity);
  250.             ((MainActivity) activity).onSectionAttached(getArguments().getInt(
  251.                     ARG_SECTION_NUMBER));
  252.         }
  253.     }
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement