Good day. I teach programming on my own. Help the guys figure out how to properly put the method into a separate class. There is a method in the main Activity that stores the String in the SharedPreferences settings:

 public void menuTobe() { listStr = new Gson().toJson(new ArrayList<Integer>(Arrayl));// сохраняю Arrayl в строку SharedPreferences.Editor editor77 = mySettings1.edit(); editor77.putString(Tobe_ArrayList, listStr);// cохраняем Arrayl в строке в SharedPreferences editor77.apply(); Log.d("", "Сохраненный Tobe_ArrayList:" + Arrayl + listStr); if (mySettings1.contains(Tobe_ArrayList)) {// если сохранен Tobe_ArrayList SharedPreferences.Editor editor = mySettings1.edit(); editor.remove("Tobe_ArrayList"); // удаляем Tobe_ArrayList editor.apply(); } } 

Here I am trying to write it in a separate class.

 public class SaveQuestions { // обьявление переменных SharedPreferences mySettings1; Activity a; String listStr; ArrayList<Integer> Arrayl; public SaveQuestions(Activity act) { a = act;// контекст через конструктор mySettings1 = act.getSharedPreferences(SPFILENAME, Context.MODE_PRIVATE); } public String menuTobe() { Arrayl = new ArrayList<>(); listStr = new Gson().toJson(new ArrayList<Integer>(Arrayl));// сохраняю Arrayl в строку SharedPreferences.Editor editor77 = mySettings1.edit(); editor77.putString(Tobe_ArrayList, listStr);// cохраняем Arrayl в строке в SharedPreferences editor77.apply(); Log.d("", "Сохраненный Tobe_ArrayList:" + Arrayl + listStr); if (mySettings1.contains(Tobe_ArrayList)) {// если сохранен Tobe_ArrayList SharedPreferences.Editor editor = mySettings1.edit(); editor.remove("Tobe_ArrayList"); // удаляем Tobe_ArrayList editor.apply(); } return listStr; } } 

I appeal to him in the main activit like this:

 SaveQuestions sv = new SaveQuestions(this); sv. menuTobe(); 

In the main menuTobe() method saves the string to SharedPreferences , but not in a separate class.

  • Cory, I can not edit the message. Generally. In the main activation, the menuTobe () method saves the string to SharedPreferences, but not in a separate class. - Eugene Neofit
  • one
    Never send a link to activate! - tim_taller
  • one
    Please do not reply with the phrase "thank you". Instead, mark the best answer as accepted (the daw opposite the selected answer). - From the queue of checks - aleksandr barakin
  • If any of the answers led you to the decision, mark it as correct (checkmark to the left of the answer). - Nick Volynkin
  • By the way, why can not edit? Click the edit button. - Nick Volynkin

1 answer 1

 Arrayl = new ArrayList<>(); listStr = new Gson().toJson(new ArrayList<Integer>(Arrayl));// сохраняю Arrayl в строку 

And nothing, that at the time of listStr initialization, is your Arrayl empty?