Get data about movies from the service using JSON. In the application I decided to add a list of favorite movies. In fact, I just need to save the id movie. The only thing that came to mind is to create a database. Whether prompt correctly to create for only one column?

  • you can simply write them to the file as an array - Mikhail Vaysman

1 answer 1

To do this, you can use SharedPreferences - https://developer.android.com/reference/android/content/SharedPreferences.html

This is usually used to store small data (up to 4kb) and application settings. Each entry is a key / value pair, but in your case the key and the value may be the same (the main thing is that there are no duplicate IDs, otherwise the value will be overwritten).

Get a copy of the repository for your list of favorite movies:

 SharedPreferences favoritesStorage = getSharedPreferences("favorites", Context.MODE_PRIVATE); 

Addition:

 favoritesStorage.edit() .putInt(String.valueOf(filmId), filmId) .apply(); 

Here you can record all the JSON by the movie ID if it is not very heavy. Then you can get your data very easily.

Get all recorded movie IDs:

 List<Integer> films = new ArrayList<>(); for (String id : favoritesStorage.getAll().keySet()) { films.add(Integer.valueOf(id)); } 

Or get information about the movie by ID:

 String json = favoritesStorage.getString(key, ""); if (!json.isEmpty()) { //.можете десериализовать, как вам угодно.// } 
  • I understand that to guarantee the issuance of information in the sequence in which it was added this method can not? - UjinUkr
  • You can keep order if you can sort the keys. - Damir Armanov