如何从共享首选项保存和检索自定义Arrailist

时间:2013-09-16 09:21:14

标签: android android-layout android-listview sharedpreferences

我想从Sharedpreference存储和检索我的自定义对象Arralist,我已经尝试但是得到了Compiletime错误

有人可以帮忙吗?

代码:

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

以下是Model类:

 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>)

2 个答案:

答案 0 :(得分:2)

列表与LT;&GT;没有实现Serializable接口。 (电影呢)。 你可以试试ArrayList(不能在这里试试)

答案 1 :(得分:1)

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