Guest User

Untitled

a guest
May 23rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. SharedPreferences.Editor editor = getSharedPreferences(prefName, MODE_PRIVATE).edit();
  2. editor.putInt("lorBucks", lorandBucks[0]);
  3. editor.commit();
  4. //But wait there's more!
  5. SharedPreferences.Editor editor2 = getSharedPreferences("BoughtStuff", MODE_PRIVATE).edit();
  6. System.out.println("IS THIS THE CULPRIT: BOUGHTITEMS SIZE: " + boughtItems.size());
  7. editor2.putInt("Items Amount", boughtItems.size());
  8. editor2.apply();
  9. for(int i = 0; i<boughtItems.size(); i++) {
  10. editor2.putString(Integer.toString(i) + " name", boughtItems.get(i).name);
  11. editor2.putInt(Integer.toString(i) + " price", boughtItems.get(i).price);
  12. editor2.putString(Integer.toString(i) + " image", boughtItems.get(i).imageName);
  13. }
  14. editor2.commit();
  15. }
  16.  
  17. //Loading and Creating Bought Items
  18. SharedPreferences sps = getSharedPreferences("BoughtStuff", MODE_PRIVATE);
  19. for(int i = 0; i<sps.getInt("Items Amount", 0); i++){
  20. System.out.println("Items Amount: " + sps.getInt("Items Amount", 0));
  21. boughtItems.add(new StoreItem(sps.getInt(Integer.toString(i) + " price", -1), sps.getString(Integer.toString(i) + " name", " "), sps.getString(Integer.toString(i) + " image", " "), layout, this));
  22. System.out.println(sps.getString("0 name", "DAMNIT!!"));
  23. System.out.println(boughtItems.get(i).name);
  24. System.out.println("NUMBER OF BOUGHT ITEMS: " + boughtItems.size());
  25. }
  26.  
  27. @Override
  28. public void onResume(){
  29. super.onResume();
  30. showItems();
  31. }
  32.  
  33. public void showItems(){
  34. for(int i = 0; i<boughtItems.size(); i++){
  35. boughtItems.get(i).setUpShop();
  36. //final StoreItem[] stores = new StoreItem[1];
  37. // stores[0] = boughtItems.get(i);
  38. // final int[] c = new int[1];
  39. // boughtItems.get(i).getIb().setOnClickListener(new View.OnClickListener(){
  40.  
  41. // @Override
  42. // public void onClick(View v){
  43. // c[0] = 1;
  44.  
  45. // }
  46. // });
  47. // if(c[0] == 1){
  48. // boughtItems.remove(i);
  49. // }
  50. }
  51. }
  52.  
  53. public void setUpShop() {
  54. ib.setImageResource(getResourceID(imageName, "drawable", act.getApplicationContext()));
  55. ib.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1200));
  56. ib.setScaleType(ImageView.ScaleType.CENTER_CROP);
  57.  
  58. TextView shopLabel = new TextView(act);
  59. shopLabel.setText(name + ": " + price + " Love Bucks");
  60. shopLabel.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
  61. shopLabel.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100));
  62. layout.addView(shopLabel);
  63. layout.addView(ib);
  64. }
  65.  
  66. SharedPreferences prefs = context.getSharedPreferences(PREF_NAME,PRIVATE_MODE);
  67. SharedPreferences.Editor editor;
  68.  
  69. public void setListToPreferance(String key, ArrayList<OBJECT_OF_MODELCLASS> list) {
  70. Gson gson = new Gson();
  71. String json = gson.toJson(list);
  72. editor = prefs.edit();
  73. editor.putString(key, json);
  74. editor.apply();
  75. }
  76.  
  77. public ArrayList<OBJECT_OF_MODELCLASS> getListProductModel(String key) {
  78. Type type = new TypeToken<ArrayList<OBJECT_OF_MODELCLASS>>() {
  79. }.getType();
  80. ArrayList<OBJECT_OF_MODELCLASS> list = AppClass.getGson().fromJson(prefs.getString(key, ""), type);
  81. if (list == null) {
  82. list = new ArrayList<OBJECT_OF_MODELCLASS>();
  83. }
  84. return list;
  85. }
Add Comment
Please, Sign In to add comment