Storage data offline in android — sharepreferences
Save your favorites collections / bookmarks offline in Android App.

This is my First story and i want to share a way to storage data in android app.
We are gonna use sharepreferences to save data offline. You could use for example to save Favorites list or Bookmarkers.
Sharepreferences:
save user preferencenes in the app. Like session of user and call values in some part of the app.
Previous knowledge: Gson, Sharepreferences, POO
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.github.bumptech.glide:glide:4.3.1'API Documentation https://rickandmortyapi.com/
API endpoint https://rickandmortyapi.com/api/character/
Dependencies:
- Gson: we help us for deserializate objects.
- Volley: HTTP library that makes networking for Android apps
- Glide: Media management and image loading framework for Android
Loading favorites data method in Fragment
private void loadFavoritesData() {
MySharedPreference sharedPreference = new MySharedPreference(getContext());
String productsFromCart = sharedPreference.retrieveFavorites();
Type type = new TypeToken<ArrayList<RickMorty>>() {
}.getType();
mFavoritesList = new Gson().fromJson(productsFromCart, type);
if (mFavoritesList == null) {
mFavoritesList = new ArrayList<>();
}
}Adapter
RickAndMortyRecyclerViewAdapter.java
Constructor
we have to pass Two Arguments (Two lists): Compare the Lists to identify and paint objects that are already favorites.
- The list of favorites items(sharepreferences): for compare with the webservices List.
- The list from the webservices
public RickAndMortyRecyclerViewAdapter(List<RickMorty> mFavorites, List<RickMorty> items) {
this.mFavoritesList = mFavorites;
this.mValues = items;
}Check if a object already exists
We have before Override the method in the Class RickMorty: compare objects with unique value (getId).
@Override()
public boolean equals(Object other) {
// This is unavoidable, since equals() must accept an Object and not something more derived
if (other instanceof RickMorty) {
// Note that I use equals() here too, otherwise, again, we will check for referential equality.
// Using equals() here allows the Model class to implement it's own version of equality, rather than
// us always checking for referential equality.
RickMorty otherProduct = (RickMorty) other;
return otherProduct.getId()==(this.getId());
}
return false;
}then in the Adapter
public static boolean checkAvailability(List<RickMorty> rickMortyList, RickMorty rickMorty) {
for (RickMorty p : rickMortyList) {
if (p.equals(rickMorty)) {
return true;
}
}
return false;
}In Adapter onBindViewHolder
if (mFavList.size()!=0){
boolean itemExists = checkAvailability(mFavList,mProductObject.get(position));
if (itemExists){
viewHolder.select_favorito.setColorFilter(ContextCompat.getColor(context,
R.color.colorRedPrimary));
}else{
viewHolder.select_favorito.clearColorFilter();
}
}viewHolder.select_favorito.setOnClickListener(new View.OnClickListener() {
int button01pos = 0;
@Override
public void onClick(View v) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
sharedPreference = new MySharedPreference(context);
if (viewHolder.select_favorito.getColorFilter() != null) {
viewHolder.select_favorito.clearColorFilter();
mFavList.remove(mProductObject.get(position));
String addNuevoItem = gson.toJson(mFavList);
sharedPreference.addProductToTheCart(addNuevoItem);
} else {
viewHolder.select_favorito.setColorFilter(ContextCompat.getColor(context,
R.color.colorRedPrimary));
mFavList.add(mProductObject.get(position));
String addNuevoItem = gson.toJson(mFavList);
sharedPreference.addProductToTheCart(addNuevoItem);
}
}
});public static boolean checkAvailability(List<Foto> stock, Foto product) {
for (Foto p : stock) {
if (p.equals(product)) {
return true;
}
}
return false;
}
Complete Adapter class
useful links