我创建了一个列表视图,允许用户勾选他们看过的列表中的电影。唯一的问题是,当我更改活动或关闭应用程序时,它会将列表视图中的所有元素重置为默认值。我想知道是否有办法使用我当前的代码保存这些更改。
public class Movies extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movies);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ListView moviesListView = (ListView)findViewById(R.id.moviesListView);
final ArrayList<String> topMovies = new ArrayList<String>(asList("The Shawshank Redemption", "The Godfather", "The Godfather: Part 2",
"The Dark Knight", "Pulp Fiction","Schlinder's List","12 Angry Men","The Lord of the Rings: The Return of the King",
"The Good, the Bad and the Ugly","Fight Club","The Lord of the Rings: The Fellowship of the Ring","Star Wars: Episode V - The Empire Strikes Back",
"Forrest Gump","Inception","One Flew Over the Cuckoo's Nest","The Lord of the Rings: The Two Towers","Goodfellas",
"The Matrix","Seven Samurai","Star Wars: Episode IV - A New Hope","City of God","Se7en","The Silence of the Lambs",
"The Usual Suspects","It's a Wonderful Life","Life is Beautiful","Leon: The Professional","Once Upon a Time in the West",
"Spirited Away","Interstellar","Saving Private Ryan","Casablanca","American History X","Psycho","City Lights","Raiders of the Lost Ark",
"Rear Window","The Intouchables","Modern Times","The Green Mile","Terminator 2: Judgement Day","The Pianist","The Departed",
"Whiplash","Back to the Future","Memento","Gladiator","Apocolypse Now","Dr. Strangelove","The Prestige"));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, topMovies);
moviesListView.setAdapter(arrayAdapter);
final MediaPlayer mPlayer = MediaPlayer.create(this,R.raw.pindrop);
moviesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(view.getAlpha()==1f) {
mPlayer.start();
Toast.makeText(getApplicationContext(), "You Watched " + topMovies.get(position), Toast.LENGTH_LONG).show();
view.animate().alpha(0.2f);
}
else{
view.animate().alpha(1f);
}
}
});
}
}
答案 0 :(得分:0)
电影的ArrayList会改变还是静态?如果列表更改或列表项被删除会发生什么。在Android开发培训中,共享首选项中的元素太多是不受欢迎的。 SQLite3数据库是最好的方法。