There is such a code:

... public static final String APP_STORAGE = "AppStorageFile"; public static final String Name_1 = "TextName7"; private SharedPreferences SFE; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SFE = getSharedPreferences(APP_STORAGE, Context.MODE_PRIVATE); ... SharedPreferences.Editor editor = SFE.edit(); editor.putString(Name_1, "Привет мир")); editor.apply(); i = 1; String Name_number = "Name_" + String.valueOf(i); Log.d("TAG_LOG", Name_number); if (SFE.contains(Name_number)) { Log.d("TAG_LOG", "Зашло"); } else{ Log.d("TAG_LOG", "Не зашло"); } } 

As a result, it is written in the logs: Name_1 Не зашло Name_1

How to write correctly so that I Зашло in the logs and at the same time be able to change the value in brackets.

For each Name_i , where i = 1.2 ... 100 there is the same code. And in order not to paint it 100 times, I decided to use String Name_number = "Name_" + String.valueOf(i);

    3 answers 3

    Do not confuse the variable name with its value. The key is the value, that is, the string that you assign to the variable ( "TextName7" ) - this is what is written in the SharedPreferences .
    What you want should look something like this:

      public static final String KEY_PREFIX = "Name_"; 

      i = 1; String nameNumber = KEY_PREFIX + i; SharedPreferences.Editor editor = SFE.edit(); editor.putString(nameNumber, "Привет мир")); editor.apply(); Log.d("TAG_LOG", nameNumber); if (SFE.contains(nameNumber)) { Log.d("TAG_LOG", "Зашло"); } else { Log.d("TAG_LOG", "Не зашло"); } 
    • Anyway Не зашло did Не зашло :( - Michael
    • @ Mikhail, it means that you are still doing something wrong - the code is 100% working. The key point of this question is that the key value must be the same in put and contains . - woesss
    • Your code is correct. It turned out that saving in Name_ 1 and saving in Name_ + i , where i=1 is not the same. Thank you so much. - Michael

    You are trying to add a string with a number without explicitly cast to a string. Try the following option:

      public static final String Name_1 = "TextName7"; private SharedPreferences SFE; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... SharedPreferences.Editor editor = SFE.edit(); editor.putString(Name_1, "Привет мир")); editor.apply(); String Name_number = "Name_" + "1"; if (SFE.contains(Name_number)) { Log.d("TAG_LOG", "Зашло") } else{ Log.d("TAG_LOG", "Не зашло") } } 

    Here in line

      String Name_number = "Name_" + "1"; 

    The rows are added and the correct string for comparison is obtained.

    • Not, too, did Не зашло . :( - Michael

    You check whether the Name1 field exists in your settings. And correctly, he deduces to you "not gone", because you have no such field. You create a field whose name you define through the variable Name1, whose value is TextName7. Make a check

     if (SFE.contains(TextName7)) 

    and you will display "Log"

    • If I write if (SFE.contains(Name_1)) , then everything works. But the fact is that I just need to be able to change the value in brackets, i.e. use Name_2 , Name_3 , etc. - Michael
    • For each Name_i , where i = 1.2 ... 100 there is the same code. And in order not to paint it 100 times, I decided to use String Name_number = "Name_" + String.valueOf(i); - Michael
    • perhaps it would be better to use an array or List for such purposes - Clarence