title: I want to retrieve the saved movies data from database and show it to user android
body: <p>I am building an app for Udacity called popular movies app which will fetch movies info from movieDB and display posters in the first activity than if the user clicked any poster it will take him to detailActivity where all the Movie detail will be displayed.</p>
  
  <p>Now I am done with stage 1, stage 2 I am supposed to give the user the ability to make a favorite movie list which will be displayed in the first activity and deatilActivity and will be fetched from and to a database.</p>
  
  <p>I already created the database and I have data saved there but I do not no how to retrieve it and display it to user kindly help me to do it.</p>
  
  <p>below is my code:</p>
  
  <p>First Activity the gridView for posters:</p>
  
  <pre><code>public class PhotoGrid extends Fragment {
  
  //Create a string array variable for every item that we are going to recive from
  // the movieDB
  String[] movieId, movieTitle, movieReleaseDate, movieVoteAverage, movieOverview, moviePosterPath;
  //use string1 to attach the poster path for every poster with the url so we can call the image
  static String[] string1;
  // define gridView here so we can use it in onPostexecute()
  GridView gridView;
  //movieUrl is used for the sortby setting
  String movieUrl;
  SQLiteDatabase db;
  databaseHelper databaseHelper;
  Cursor cursor;
  ContentProvider contentProvider;
  
  
  
  public PhotoGrid() {
  
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // Add this line in order for this fragment to handle menu events.
      setHasOptionsMenu(true);
  }
  
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      inflater.inflate(R.menu.menu, menu);
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_refresh) {
          updateMovie();
          return true;
      } else if (id == R.id.action_settings) {
          //if action_setting clicked SettingActivity will start
          Intent intent = new Intent(getActivity(), SettingActivity.class);
          startActivity(intent);
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  public void updateMovie() {
      FetchMoviesPosters movieTask = new FetchMoviesPosters();
      //make popularity as the default order or call for movieposters in settings
      SharedPreferences sharedPreferences = PreferenceManager
              .getDefaultSharedPreferences(getActivity());
      String sortBy = sharedPreferences.getString(getString(R.string.pref_sortby_key),
              getString(R.string.pref_sortby_default));
      movieTask.execute(sortBy);
  }
  
  @Override
  public void onStart() {
      super.onStart();
      //update movies list on start
      updateMovie();
  }
  
  
  @Override
  public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
  
  
      final View rootView = inflater.inflate(R.layout.fragment_photo_grid, container, false);
  
      databaseHelper = new databaseHelper(getActivity(),MovieContract.MovieEntry.TABLE_NAME,null,2);
  
      db = databaseHelper.getReadableDatabase();
  
      gridView = (GridView) rootView.findViewById(R.id.grid_view);
      updateMovie();
      gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  
              //Here handle the on poster click action by assigning the clicked poster info
              //to strings and send them to detail activity with different keys to be able to
              // control each item alone
              String movieIDText =  movieId[i];
              String movieTitleText =   movieTitle[i];
              String movieOverViewText =  movieOverview[i];
              String movieReleaseDateText =  movieReleaseDate[i];
              String movieRatingText = movieVoteAverage[i];
              String movieDetailImage = moviePosterPath[i];
  
              Intent intent = new Intent(getActivity(), DetailActivity.class);
              intent.putExtra("movie_id", movieIDText);
              intent.putExtra("movie_overview", movieOverViewText);
              intent.putExtra("movie_title", movieTitleText);
              intent.putExtra("movie_release_date", movieReleaseDateText);
              intent.putExtra("movie_rating", movieRatingText);
              intent.putExtra("image_path", movieDetailImage);
              startActivity(intent);
          }
      });
  
      return rootView;
  }
  
  
  //ImageAdapter is used to control images dimensions and load them in the
  // imageview using Picasso
  public class ImageAdapter extends BaseAdapter {
  
      private Context mContext;
  
      private String[] mThumbIds;
  
      public ImageAdapter(Context c, String[] str2) {
  
          mContext = c;
          mThumbIds = str2;
      }
  
      @Override
      public int getCount() {
          if (mThumbIds != null) {
              return mThumbIds.length;
          } else {
              return 0;
          }
      }
  
      @Override
      public Object getItem(int position) {
          return null;
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ImageView imageView;
          if (convertView == null) {
              // if it's not recycled, initialize some attributes
              imageView = new ImageView(mContext);
              imageView.setLayoutParams(new GridView.LayoutParams(700, 1200));
              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
              imageView.setPadding(4, 4, 4, 4);
  
  
          } else {
              imageView = (ImageView) convertView;
          }
  
          Picasso.with(mContext).load(mThumbIds[position]).into(imageView);
  
          return imageView;
      }
  }
  
  public class FetchMoviesPosters extends AsyncTask&lt;String, Void, String[]&gt; {
      private final String LOG_TAG = FetchMoviesPosters.class.getSimpleName();
  
      //in this function the different order settings are defined
      private String setOrder(String sortBy) {
          if (sortBy.equals(getString(R.string.pref_sorting_popularity))) {
              movieUrl = "https://api.themoviedb.org/3/movie/popular?";
          } else if (sortBy.equals(getString(R.string.pref_sorting_highest_rating))) {
              movieUrl = "https://api.themoviedb.org/3/movie/top_rated?";
          }
          else if (sortBy.equals(getString(R.string.pref_sorting_favorite))){
  
  
              cursor = databaseHelper.retrieveData(db);
              if (cursor.moveToFirst()){
                  do {
  
                      String id, title, overView, releaseDate, rating, posterPath;
                      id = cursor.getString(0);
                      title = cursor.getString(1);
                      overView = cursor.getString(2);
                      releaseDate = cursor.getString(3);
                      rating = cursor.getString(4);
                      posterPath = cursor.getString(5);
  
                       contentProvider = new ContentProvider(id , title , overView
                      , releaseDate, rating , posterPath);
  
  
  
  
                  }while (cursor.moveToNext());
  
  
              }
  
  
  
          }
          return sortBy;
      }
  
  
      private String[] MoviesJasonPrase(String moviesPosterStr ) throws JSONException {
  
          final String M_Result = "results";
          final String M_ID = "id";
          final String M_Title = "original_title";
          final String M_Release = "release_date";
          final String M_Vote = "vote_average";
          final String M_OverV = "overview";
          final String M_Poster = "poster_path";
  
          JSONObject moviesJson = new JSONObject(moviesPosterStr);
          JSONArray resultsArray = moviesJson.getJSONArray(M_Result);
          movieId = new String[resultsArray.length()];
          movieTitle = new String[resultsArray.length()];
          movieReleaseDate = new String[resultsArray.length()];
          movieVoteAverage = new String[resultsArray.length()];
          movieOverview = new String[resultsArray.length()];
          moviePosterPath = new String[resultsArray.length()];
  
          for (int i = 0; i &lt; resultsArray.length(); i++) {
  
              JSONObject movie = resultsArray.getJSONObject(i);
              movieId[i] = movie.getString(M_ID);
              movieTitle[i] = movie.getString(M_Title);
              movieReleaseDate[i] = movie.getString(M_Release);
              movieVoteAverage[i] = movie.getString(M_Vote);
              movieOverview[i] = movie.getString(M_OverV);
              moviePosterPath[i] = movie.getString(M_Poster);
  
  
  
          }
  
          return moviePosterPath;
      }
  
      @Override
      protected String[] doInBackground(String... params) {
  
          if (params.length == 0) {
              return null;
          }
  
  
          HttpURLConnection urlConnection = null;
          BufferedReader reader = null;
  
          // Will contain the raw JSON response as a string.
          String moviePostersJsonStr = null;
  
          try {
  
              SharedPreferences sharedPreferences = PreferenceManager
                      .getDefaultSharedPreferences(getActivity());
              String sortBy = sharedPreferences.getString(getString(R.string.pref_sortby_key),
                      getString(R.string.pref_sorting_popularity));
              setOrder(sortBy);
  
              final String APPID_PARAM = "api_key";
  
              Uri builtUri = Uri.parse(movieUrl).buildUpon()
                      .appendQueryParameter(APPID_PARAM,   BuildConfig.THE_MOVIE_DB)
                      .build();
  
  
              URL url = new URL(builtUri.toString());
  
              Log.v(LOG_TAG, "Built URI " + builtUri.toString());
              // Create the request to TheMovieDB, and open the connection
              urlConnection = (HttpURLConnection) url.openConnection();
              urlConnection.setRequestMethod("GET");
              urlConnection.connect();
  
              // Read the input stream into a String
              InputStream inputStream = urlConnection.getInputStream();
              StringBuilder buffer = new StringBuilder();
  
  
              if (inputStream == null) {
                  // Nothing to do.
                  return null;
              }
  
              reader = new BufferedReader(new InputStreamReader(inputStream));
  
              String line;
              while ((line = reader.readLine()) != null) {
  
                  buffer.append(line).append("\n");
              }
  
              if (buffer.length() == 0) {
                  // Stream was empty.  No point in parsing.
                  return null;
              }
  
              moviePostersJsonStr = buffer.toString();
  
          } catch (IOException e) {
              Log.e("PhotoGrid", "Error ", e);
              // If the code didn't successfully get the weather data, there's    no point in attemping
              // to parse it.
              return null;
          } finally {
              if (urlConnection != null) {
                  urlConnection.disconnect();
              }
              if (reader != null) {
                  try {
                      reader.close();
                  } catch (final IOException e) {
                      Log.e("PhotoGrid", "Error closing stream", e);
                  }
              }
  
          }
  
          try {
              return MoviesJasonPrase(moviePostersJsonStr);
          } catch (JSONException e) {
              Log.e(LOG_TAG, e.getMessage(), e);
              e.printStackTrace();
          }
  
          return null;
  
      }
  
  
      @Override
      protected void onPostExecute(String[] Strings) {
          if (Strings != null) {
  
              string1 = new String[Strings.length];
              for (int i = 0; i &lt; Strings.length; i++) {
                  //receive poster images path
                  String[] getImage = Strings[i].split("-");
                  //concatenate path to url "http://image.tmdb.org/t/p/w185/"
                  string1[i] = "http://image.tmdb.org/t/p/w185/" + getImage[0];
              }
              ImageAdapter imageAdapter = new ImageAdapter(getActivity(),   string1);
              //put images after going though the adapter in the gridview
              gridView.setAdapter(imageAdapter);
  
           }
        }
     }
  }
  </code></pre>
  
  <p>The detailActivity:</p>
  
  <pre><code>public class DetailFragment extends Fragment {
  
  String ID;
  String title;
  String overView;
  String releaseDate;
  String rating;
  String posterPath;
  String movieKey;
  databaseHelper myDB ;
  ImageButton favorite;
  public DetailFragment() {
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // Add this line in order for this fragment to handle menu events.
      setHasOptionsMenu(true);
  }
  
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      inflater.inflate(R.menu.menu_detail, menu);
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          Intent intent = new Intent(getActivity(), SettingActivity.class);
          startActivity(intent);
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      final View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
  
      myDB = new databaseHelper(getActivity(), MovieContract.MovieEntry.TABLE_NAME,null,2);
  
      favorite = (ImageButton) rootView.findViewById(R.id.favorite);
  
      final Intent intent = getActivity().getIntent();
      // The detail Activity called via intent.  Inspect the intent for
      // movies data using movie ID.
      if (intent != null &amp;&amp; intent.hasExtra("movie_id")) {
          //if true put each item in a textview and load the poster in imageView
          ID = intent.getStringExtra("movie_id");
          title = intent.getStringExtra("movie_title");
          ((TextView) rootView.findViewById(R.id.title_text))
                  .setText(title);
         overView = intent.getStringExtra("movie_overview");
          ((TextView) rootView.findViewById(R.id.overview_text))
                  .setText(overView);
          releaseDate = intent.getStringExtra("movie_release_date");
          ((TextView) rootView.findViewById(R.id.release_date_text))
                  .setText(releaseDate);
              rating = intent.getStringExtra("movie_rating");
          ((TextView) rootView.findViewById(R.id.rating_text))
                  .setText(rating);
  
  
             posterPath = intent.getStringExtra("image_path");
          String posterImage = "http://image.tmdb.org/t/p/w185/" + posterPath;
          ImageView imageView = (ImageView) rootView.findViewById(R.id.detail_image);
          Picasso.with(getActivity()).load(posterImage).resize(500, 800).into(imageView);
  
      }
  
      Button button = (Button) rootView.findViewById(R.id.play);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              playTrailer();
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setData(Uri.parse(String.valueOf("http://www.youtube.com/watch?v="+ movieKey)));
              startActivity(intent);
          }
      });
  
      Button button1 = (Button) rootView.findViewById(R.id.open_reviews);
      button1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String id = ID;
              Intent intent1 = new Intent(getActivity(), ReviewActivity.class);
              intent1.putExtra("movie_id", id);
              startActivity(intent1);
  
          }
      });
  
       addData();
  
      return rootView;
  }
  
  public void playTrailer() {
  
      FetchMoviesTrailer fetchMoviesTrailer = new FetchMoviesTrailer();
      fetchMoviesTrailer.execute(ID);
  
  }
  
  public void addData(){
      favorite.setOnClickListener(
              new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                   boolean isInsearted = myDB.insert(ID, title, overView, releaseDate,
                           rating, posterPath);
  
                      if (isInsearted)
                          Toast.makeText(getActivity(),"Added to Favorite", Toast.LENGTH_SHORT)
                                  .show();
                      else
                          Toast.makeText(getActivity(),"Not Added to Favorite", Toast.LENGTH_SHORT)
                                  .show();
                  }
              }
      );
  }
  
  public class FetchMoviesTrailer extends AsyncTask&lt;String, Void, String[]&gt; {
      private final String LOG_TAG = FetchMoviesTrailer.class.getSimpleName();
  
      //in this function the different order settings are defined
  
  
      private String[] MoviesJasonPrase(String moviesTrailerStr) throws JSONException {
  
          final String T_Result = "results";
          final String T_key = "key";
  
          JSONObject moviesJson = new JSONObject(moviesTrailerStr);
          JSONArray resultsArray = moviesJson.getJSONArray(T_Result);
  
          String[] strings = new String[resultsArray.length()];
  
          for (int i = 0; i &lt; resultsArray.length(); i++) {
  
              JSONObject movie = resultsArray.getJSONObject(i);
              movieKey = movie.getString(T_key);
  
  
              strings[i] = movieKey;
  
          }
  
          return strings;
      }
  
      @Override
      protected String[] doInBackground(String... params) {
  
          if (params.length == 0) {
              return null;
          }
  
  
          HttpURLConnection urlConnection = null;
          BufferedReader reader = null;
  
          // Will contain the raw JSON response as a string.
          String movieTrailerJsonStr = null;
  
          try {
  
              final String APPID_PARAM = "api_key";
              final String Traile_Url = "http://api.themoviedb.org/3/movie/" + ID
                      + "/videos?";
  
              Uri builtUri = Uri.parse(Traile_Url).buildUpon()
                      .appendQueryParameter(APPID_PARAM, BuildConfig.THE_MOVIE_DB)
                      .build();
  
  
              URL url = new URL(builtUri.toString());
  
              Log.v(LOG_TAG, "Built URI " + builtUri.toString());
              // Create the request to TheMovieDB, and open the connection
              urlConnection = (HttpURLConnection) url.openConnection();
              urlConnection.setRequestMethod("GET");
              urlConnection.connect();
  
              // Read the input stream into a String
              InputStream inputStream = urlConnection.getInputStream();
              StringBuilder buffer = new StringBuilder();
  
  
              if (inputStream == null) {
                  // Nothing to do.
                  return null;
              }
  
              reader = new BufferedReader(new InputStreamReader(inputStream));
  
              String line;
              while ((line = reader.readLine()) != null) {
  
                  buffer.append(line).append("\n");
              }
  
              if (buffer.length() == 0) {
                  // Stream was empty.  No point in parsing.
                  return null;
              }
  
              movieTrailerJsonStr = buffer.toString();
  
  
          } catch (IOException e) {
              Log.e("PhotoGrid", "Error ", e);
              // If the code didn't successfully get the weather data, there's no point in attemping
              // to parse it.
              return null;
          } finally {
              if (urlConnection != null) {
                  urlConnection.disconnect();
              }
              if (reader != null) {
                  try {
                      reader.close();
                  } catch (final IOException e) {
                      Log.e("PhotoGrid", "Error closing stream", e);
                  }
              }
  
          }
  
          try {
              return MoviesJasonPrase(movieTrailerJsonStr);
          } catch (JSONException e) {
              Log.e(LOG_TAG, e.getMessage(), e);
              e.printStackTrace();
          }
  
          return null;
  
      }
  
      @Override
      protected void onPostExecute(String[] strings) {
          super.onPostExecute(strings);
      }
    }
  
  }
  </code></pre>
  
  <p>The DataBase Helper:
      public class databaseHelper extends SQLiteOpenHelper{</p>
  
  <pre><code>SQLiteDatabase db ;
  public static final int DATABASE_VERSION = 2;
  public static final String DATABASE_NAME = "FavoriteMovies.db";
  
   public databaseHelper(Context context, String name,   SQLiteDatabase.CursorFactory factory, int version) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      final String SQL_CREATE_Movie_TABLE = "CREATE TABLE " +  MovieEntry.TABLE_NAME + " (" +
              MovieEntry.ID_COLUMAN + " TEXT PRIMARY KEY," +
              MovieEntry.TITLE_COLUMAN + " TEXT NOT NULL, " +
              MovieEntry.OVERVIEW_COLUMAN + " TEXT NOT NULL, " +
              MovieEntry.RELEASE_DATE_COLUMAN + " TEXT NOT NULL, " +
              MovieEntry.RATING_COLUMAN + " TEXT NOT NULL, " +
              MovieEntry.POSTAR_PATH_COLUMAN+ " TEXT NOT NULL " +
              " );";
  
      db.execSQL(SQL_CREATE_Movie_TABLE);
  
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
     db.execSQL("DROP TABLE IF EXISTS" + MovieEntry.TABLE_NAME);
      onCreate(db);
  }
  public boolean insert(String id, String title , String overView , String date, String rating,
                        String poster){
  
      db = super.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put(MovieEntry.ID_COLUMAN,id);
      contentValues.put(MovieEntry.TITLE_COLUMAN,title);
      contentValues.put(MovieEntry.OVERVIEW_COLUMAN,overView);
      contentValues.put(MovieEntry.RELEASE_DATE_COLUMAN,date);
      contentValues.put(MovieEntry.RATING_COLUMAN,rating);
      contentValues.put(MovieEntry.POSTAR_PATH_COLUMAN, poster);
  
      long isAdded = db.insert(MovieEntry.TABLE_NAME, null ,contentValues);
      if (isAdded == -1) {
          return false;
      }
      else
          return true;
  }
  public Cursor retrieveData(SQLiteDatabase db){
  
      Cursor cursor;
  
      String[] projection = {MovieEntry.ID_COLUMAN, MovieEntry.TITLE_COLUMAN,
              MovieEntry.OVERVIEW_COLUMAN, MovieEntry.RELEASE_DATE_COLUMAN, MovieEntry.RATING_COLUMAN,
              MovieEntry.POSTAR_PATH_COLUMAN};
     cursor = db.query(MovieEntry.TABLE_NAME, projection, null,null,null,null,null);
  
      return cursor;
   }
  }
  </code></pre>
  
  <p>The DataBase Contract:</p>
  
  <pre><code> public class MovieContract {
  
  public MovieContract(){}
  
  public static abstract class MovieEntry implements BaseColumns{
  
      public static final String TABLE_NAME = "favorite";
      public static final String ID_COLUMAN = "ID";
      public static final String TITLE_COLUMAN = "title";
      public static final String OVERVIEW_COLUMAN = "overView";
      public static final String RELEASE_DATE_COLUMAN = "releaseDate";
      public static final String RATING_COLUMAN = "rating";
      public static final String POSTAR_PATH_COLUMAN = "posterPath";
  
   }
  }
  </code></pre>
  
  <p>The content Provider:</p>
  
  <pre><code>public class ContentProvider {
  private String id;
  private String title;
  private String overView;
  private String releaseDate;
  private String rating;
  private String posterPath;
  
  public ContentProvider(String ID, String Title,String OverView, String ReleaseDate,
                         String Rating, String PosterPath){
      this.id = ID;
      this.title = Title;
      this.overView = OverView;
      this.releaseDate = ReleaseDate;
      this.rating = Rating;
      this.posterPath = PosterPath;
  
  
  }
  public String getId() {
      return id;
  }
  
  public void setId(String id) {
      this.id = id;
  }
  
  public String getTitle() {
      return title;
  }
  
  public void setTitle(String title) {
      this.title = title;
  }
  
  public String getOverView() {
      return overView;
  }
  
  public void setOverView(String overView) {
      this.overView = overView;
  }
  
  public String getReleaseDate() {
      return releaseDate;
  }
  
  public void setReleaseDate(String releaseDate) {
      this.releaseDate = releaseDate;
  }
  
  public String getRating() {
      return rating;
  }
  
  public void setRating(String rating) {
      this.rating = rating;
  }
  
  public String getPosterPath() {
      return posterPath;
  }
  
  public void setPosterPath(String posterPath) {
      this.posterPath = posterPath;
     }
  
  
  }
  </code></pre>