0

我想从 Sharedpreference 中存储和检索我的自定义对象 Arralist,我已经尝试过但收到编译时错误

有人可以帮忙吗?

代码:

 @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();
            }
}

这是要成为的模型类:

 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;
        }

编译时错误:

The method serialize(Serializable) in the type ObjectSerializer is not applicable for the 
arguments (List<Movie>)
4

2 回答 2

2

List<> 没有实现 Serializable 接口。(电影有)。你可以试试 ArrayList (不能在这里尝试)

于 2013-09-16T09:29:39.837 回答
1

而不是List<Movie> movieList;使用ArrayList<Movie> movieList;

于 2013-09-16T09:39:25.813 回答