コード例 #1
0
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    setHasOptionsMenu(true);

    /** ******************** new code***************** */
    mGridView = (GridView) rootView.findViewById(R.id.gridView);
    mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);

    // Initialize with empty data
    mGridData = new ArrayList<>();
    mGridAdapter = new ImageAdapter(getActivity(), R.layout.grid_item_layout, mGridData);
    mGridView.setAdapter(mGridAdapter);

    // Grid view click event
    mGridView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            // Get item at position
            Movie item = (Movie) parent.getItemAtPosition(position);

            Intent intent = new Intent(getActivity(), detailMovie.class);

            // Pass the movie details DetailsActivity
            intent
                .putExtra("title", item.getTitle())
                .putExtra("overview", item.getOverview())
                .putExtra("rating", item.getRating())
                .putExtra("rDate", item.getReleaseDate())
                .putExtra("id", item.getMovieID())
                .putExtra("pURL", item.getPosterURL())
                .putExtra("pURL", item.getPosterURL());

            mPosition = position;

            mListener.OnItemClicked(item);

            // Start details activity
            startActivity(intent);
          }
        });

    // --------------------

    if (savedInstanceState == null || !savedInstanceState.containsKey(SELECTED_KEY)) {

      mProgressBar.setVisibility(View.VISIBLE);
      // getting the setting value
      String Sort_By = getPreferredSort(getActivity());
      // favourite
      String Fav = getString(R.string.pref_sort_favourite);
      String pop = getString(R.string.pref_sort_popular);
      String rate = getString(R.string.pref_sort_rate);

      Toast.makeText(getActivity(), "hey_there " + Sort_By, Toast.LENGTH_SHORT).show();

      if (Sort_By.contentEquals(Fav)) {
        SharedPreferences pref =
            getActivity()
                .getSharedPreferences(getString(R.string.pref_movie_name), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        Log.d("Entered", "favourtie");
        String fM = pref.getString(getString(R.string.pref_movie_key), "");
        // Log.d("Getting Movie ",fM );

        String fMovies[];
        if (fM != null) {

          fMovies = fM.split("#");
          mGridData.clear();
          String rMovies[], vMovies[];
          reviews = new ArrayList<>();
          videos = new ArrayList<>();
          String[] movieDetails;
          String[] reviewDetail;
          String[] videoDetail;
          Movie movieObj;
          Review review;
          Video video;

          for (int i = 0; i < fMovies.length; i++) {
            movieDetails = fMovies[i].split("|");
            movieObj = new Movie();

            movieObj.setMovieID(movieDetails[0]);
            movieObj.setPosterURL(movieDetails[1]);
            movieObj.setBackgroundUrl(movieDetails[2]);
            movieObj.setTitle(movieDetails[3]);
            movieObj.setOverview(movieDetails[4]);
            movieObj.setReleaseDate(movieDetails[5]);
            movieObj.setRating(movieDetails[6]);

            //                        Log.d("Movie From Prefrence",
            //                                " ---  "+movieDetails[0] +
            //                                        " --- "+movieDetails[1] +
            //                                        " ---"+movieDetails[2] +
            //                                        " ---- "+movieDetails[3] +
            //                                        " ---  "+movieDetails[4] +
            //                                        " --- "+movieDetails[5] +
            //                                        " --- "+movieDetails[6] );

            rMovies = pref.getString(getString(R.string.pref_movie_name), null).split("#");
            for (int j = 0; j < rMovies.length; j++) {
              reviewDetail = rMovies[i].split("|");
              review = new Review();
              //                        Log.d("Review", "Author "+reviewDetail[0]
              //                                        + "Content "+reviewDetail[1]);
              review.setAuthor(reviewDetail[0]);
              review.setContent(reviewDetail[1]);

              reviews.add(review);
            }
            vMovies = pref.getString(getString(R.string.pref_movie_name), null).split("#");
            for (int j = 0; j < vMovies.length; j++) {
              videoDetail = vMovies[i].split("|");
              video = new Video();

              //                        Log.d("Videos", "Title "+videoDetail[0]
              //                                + "URL "+videoDetail[1]);
              video.setName(videoDetail[0]);
              video.setUrl(videoDetail[1]);

              videos.add(video);
            }

            mGridData.add(movieObj);
          }
          //                for(int q=0 ; q<mGridData.size() ; q++){
          //
          //                    Log.d("Favorite "+ q, "Backgorund
          // "+mGridData.get(q).getBackgroundUrl()
          //                                        + "Date " +mGridData.get(q).getReleaseDate()
          //                                        + "Movie ID :" +mGridData.get(q).getMovieID()
          //                                        + "Rting: "+mGridData.get(q).getRating()
          //                                        +"Title" + mGridData.get(q).getTitle()
          //                    );
          //                }
          mGridAdapter.setGridData(mGridData);
          //                Log.d("Favorite", mGridData.toString());
          Toast.makeText(getActivity(), "Here are your Movies", Toast.LENGTH_SHORT).show();

        } else {
          Toast.makeText(getActivity(), "You Don't have any Favourite", Toast.LENGTH_SHORT).show();
        }
      } else if (Sort_By.contentEquals(pop) || Sort_By.contentEquals(rate)) {
        Toast.makeText(getActivity(), "hey_there " + Sort_By, Toast.LENGTH_SHORT).show();
        Log.d("Sort BY", Sort_By);
        mGridData.clear();
        new FetchMoviesTask().execute(Sort_By);
      } else {
        mGridData.clear();
        new FetchMoviesTask().execute(pop);
      }

    } else {

      Toast.makeText(getActivity(), "hey  Else ", Toast.LENGTH_SHORT).show();
      mGridData = savedInstanceState.getParcelableArrayList(SELECTED_KEY);

      mGridAdapter = new ImageAdapter(getActivity(), R.layout.grid_item_layout, mGridData);
      mGridView.setAdapter(mGridAdapter);
      Log.d("Main Grid", mGridData.toString());
      // set
    }
    // ----------------

    /** ********************************************** */
    return rootView;
  }
コード例 #2
0
    /**
     * Take the String representing the complete forecast in JSON Format and pull out the data we
     * need to construct the Strings needed for the wireframes.
     *
     * <p>Fortunately parsing is easy: constructor takes the JSON string and converts it into an
     * Object hierarchy for us.
     */
    private String[] getMovieDataFromJson(String MovieJsonStr) throws JSONException {

      final String Results_ID = "results";
      final String Movie_ID = "id";
      final String Title_ID = "original_title";
      final String Poster_URL_ID = "poster_path";
      final String Background_URL_ID = "backdrop_path";
      final String OverView_ID = "overview";
      final String Rating_ID = "vote_average";
      final String release_Date_ID = "release_date";
      final String baseImageUrl = "http://image.tmdb.org/t/p/";
      final String imageSizeForUrl = "w185/";

      JSONObject MovieJson = new JSONObject(MovieJsonStr);
      JSONArray moviesArray = MovieJson.getJSONArray(Results_ID);

      String[] resultStrs = new String[moviesArray.length()];
      String title, posterURL, overview, rating, releaseDate, backgroundURL, movieID;

      Movie movieObj;
      Movie item = new Movie();
      for (int i = 0; i < moviesArray.length(); i++) {
        // Get the JSON object representing the day
        JSONObject MovieDetails = moviesArray.getJSONObject(i);

        title = MovieDetails.getString(Title_ID);
        posterURL = MovieDetails.getString(Poster_URL_ID);
        overview = MovieDetails.getString(OverView_ID);
        rating = MovieDetails.getString(Rating_ID);
        releaseDate = MovieDetails.getString(release_Date_ID);
        backgroundURL = MovieDetails.getString(Background_URL_ID);
        movieID = MovieDetails.getString(Movie_ID);

        posterURL = baseImageUrl + imageSizeForUrl + posterURL;
        backgroundURL = baseImageUrl + imageSizeForUrl + backgroundURL;

        String details =
            posterURL
                + "|"
                + backgroundURL
                + "|"
                + title
                + "|"
                + overview
                + "|"
                + rating
                + "|"
                + releaseDate
                + "|"
                + movieID;

        movieObj = new Movie();
        movieObj.setTitle(title);
        movieObj.setOverview(overview);
        movieObj.setRating(rating);
        movieObj.setReleaseDate(releaseDate);
        movieObj.setBackgroundUrl(backgroundURL);
        movieObj.setPosterURL(posterURL);
        movieObj.setMovieID(movieID);

        Log.v(LOG_TAG, "Movie string: " + details);
        resultStrs[i] = details;

        mGridData.add(movieObj);
      }
      return resultStrs;
    }