0

I want to Store and retrieve my Custom object Arralist from Sharedpreference,i have tried but getting Compiletime Error

Could any one help?

Code:

 @Override protected void onPause() {
    List<Movie> movieList;
            super.onPause();        


                //save the movie list to preference
                SharedPreferences preferences = getPreferences(MODE_PRIVATE);           
                Editor editor = preferences.edit();
                try {
                    editor.putString("movies", org.apache.pig.impl.util.ObjectSerializer.serialize(movieList));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                editor.commit();
            }
}

Here is the Model class which is to be :

 public class Movie implements Serializable {

        private static final long serialVersionUID = 1L;
        private String MovieName;
        private String MovieActor;
        private String MovieActress;
        private String MovieDirector;
        private String MovieImage;
        private String MovieDescription;
        private String MovieWatchLink;
        public String getMovieName() {
            return MovieName;
        }
        public void setMovieName(String movieName) {
            MovieName = movieName;
        }
        public String getMovieActor() {
            return MovieActor;
        }
        public void setMovieActor(String movieActor) {
            MovieActor = movieActor;
        }
        public String getMovieActress() {
            return MovieActress;
        }
        public void setMovieActress(String movieActress) {
            MovieActress = movieActress;
        }
        public String getMovieDirector() {
            return MovieDirector;
        }
        public void setMovieDirector(String movieDirector) {
            MovieDirector = movieDirector;
        }
        public String getMovieImage() {
            return MovieImage;
        }
        public void setMovieImage(String movieImage) {
            MovieImage = movieImage;
        }
        public String getMovieDescription() {
            return MovieDescription;
        }
        public void setMovieDescription(String movieDescription) {
            MovieDescription = movieDescription;
        }
        public String getMovieWatchLink() {
            return MovieWatchLink;
        }
        public void setMovieWatchLink(String movieWatchLink) {
            MovieWatchLink = movieWatchLink;
        }

Compile time Error:

The method serialize(Serializable) in the type ObjectSerializer is not applicable for the 
arguments (List<Movie>)
user2580568
  • 85
  • 3
  • 11
  • possible duplicate of [Saving Bundle object into shared preference](http://stackoverflow.com/questions/18821405/saving-bundle-object-into-shared-preference) – Developer Sep 16 '13 at 09:26
  • This has nothing to do with the SharedPreference. – RvdK Sep 16 '13 at 09:27

2 Answers2

2

List<> does not implement the Serializable interface. (Movie does). You could try ArrayList (cannot try it out here)

RvdK
  • 19,580
  • 4
  • 64
  • 107
1

Instead of List<Movie> movieList; use ArrayList<Movie> movieList;

Sunny
  • 14,522
  • 15
  • 84
  • 129