Storage data offline in android — sharepreferences

Save your favorites collections / bookmarks offline in Android App.

Roger Colquehuanca
Nov 2 · 3 min read

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:

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:

  • 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

Constructor

  • 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

@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

https://www.youtube.com/watch?v=jcliHGR3CHo

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade