Advertisement
Guest User

SharedPreference

a guest
May 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. //Adapter saved data
  2.  
  3. String key = "Key";
  4. List<CartProductListPojo> cartProductListPojos = new ArrayList();
  5. cartProductListPojos.add(new CartProductListPojo( getItemId.getProductImage(),
  6. getItemId.getProductName(),
  7. getItemId.getProductBrandName(), getItemId.getProductCategory(), 1,
  8. getItemId.getProductPrice()));
  9.  
  10. SharedPreferences shref;
  11. SharedPreferences.Editor editor;
  12. shref = context.getSharedPreferences("Name", Context.MODE_PRIVATE);
  13.  
  14. Gson gson = new Gson();
  15. String json = gson.toJson(cartProductListPojos);
  16.  
  17. editor = shref.edit();
  18. /*editor.remove(key).commit();*/
  19. editor.putString(key, json);
  20. editor.commit();
  21.  
  22. //CartActivity retrieve data
  23.  
  24. SharedPreferences sharedPreferences = getSharedPreferences("Name", MODE_PRIVATE);
  25. String key = "Key";
  26. Gson gson = new Gson();
  27. String response = sharedPreferences.getString(key, "");
  28. List<CartProductListPojo> cartProductListPojo = gson.fromJson(response,
  29. new TypeToken<List<CartProductListPojo>>() {}.getType());
  30.  
  31. for (int i = 0; i < cartProductListPojo.size(); i++){
  32. productsName = cartProductListPojo.get(i).getProductName();
  33. Toast.makeText(this, productsName, Toast.LENGTH_SHORT).show();
  34.  
  35. cartProductListPojos.add(new CartProductListPojo(productsImage, productsName,
  36. productsBrandName, productsCategory, 1, productsPrice));
  37.  
  38. cartProductListAdapter = new CartProductListAdapter(this, cartProductListPojos);
  39. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  40. recyclerView.setAdapter(cartProductListAdapter);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement