0

I have created a listview that allows users to tick off movies on the list that they have seen. The only problem is that when I change activity or close the app it resets all elements in the listview to default. I'm wondering if there's a way to save these changes using my current code.

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

            }
        }
    });
}

}
  • You need to save somewhere (generally in a database) these changes to keep track whether a movie is marked as seen or not. – kevinkl3 Feb 09 '16 at 21:12
  • The simplest when to save data on android is using SharedPreferences. But if you have some knowledge about databases, you can use SQLite. Here are some links to get you started: http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – Rosário Pereira Fernandes Feb 09 '16 at 21:13
  • Thanks guys. Would shared preferences be able to save all 50 rows in the listview? – BelovedAunt Feb 09 '16 at 22:03

1 Answers1

0

Is your ArrayList of movies going to change or is it static? What happens if the list changes or a list item is removed. Too many elements in shared preferences was frowned on in android development training. A SQLite3 database would be the best approach.

MrMagoo
  • 67
  • 1
  • 12
  • Thanks. It's all going to stay the same. Considering that the only thing changing in the elements is the alpha, is it possible to store those changes in SQLlite? – BelovedAunt Feb 10 '16 at 02:02
  • Yes, it's fairly easy to define a table that contains columns for the information you want to store. Reading from that table or tables is fairly easy as well. – MrMagoo Feb 11 '16 at 02:17