Advertisement
ethancannon10

Add To Favourites

Jun 5th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. private void addToFavorites(MenuItem item) {
  2.         SharedPreferences sharedPreferences = new SharedPreferences(getApplicationContext());
  3.         sharedPreferences.saveBoolean(title.toLowerCase().replaceAll(" ", "_").trim(), !sharedPreferences.getBoolean(title.toLowerCase().replaceAll(" ", "_").trim(), false));
  4.         if (sharedPreferences.getBoolean(title.toLowerCase().replaceAll(" ", "_").trim(), false)) {
  5.             item.setIcon(getResources().getDrawable(R.drawable.ic_action_favorite));
  6.             Toast.makeText(getApplicationContext(), R.string.added_to_favorites, Toast.LENGTH_SHORT).show();
  7.         }
  8.         else {
  9.             item.setIcon(getResources().getDrawable(R.drawable.ic_action_favorite_outline));
  10.             Toast.makeText(getApplicationContext(), R.string.removed_from_favorites, Toast.LENGTH_SHORT).show();
  11.         }
  12.         MainActivity.updateDrawer(getApplicationContext());
  13.     }
  14.  
  15.  
  16. //Then to loop through the array and check if that item exists in Shared Preferences
  17.  for (int ii = 0; ii < jsonArrayWallpaper.length(); ii++) {
  18.                         if (sharedPreferences.getBoolean(jsonArrayWallpaper.getJSONObject(ii).getString("title").toLowerCase().replaceAll(" ", "_").trim(), false)) {
  19.                             icon.add(jsonArrayWallpaper.getJSONObject(ii).getString("url"));
  20.                             title.add(jsonArrayWallpaper.getJSONObject(ii).getString("title"));
  21.                             author.add(jsonArrayWallpaper.getJSONObject(ii).getString("author"));
  22.                         }
  23.                     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement