Я хочу, чтобы моя кнопка показывала «catch» в самом начале, если нет ключа, совпадающего с именем текущего покемона, и «release», если он есть, и его значение равно true. Я отправляю "false" по умолчанию с помощью getBoolean. Но по какой-то причине первый текст, который я вижу с каждым новым покемоном, - «релиз». Что может быть не так?
Редактировать: я включаю весь метод onCreate и метод onClick кнопки вместе с макетом.
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Catch"
android:onClick="toggleCatch"
android:visibility="visible" />
SharedPreferences catchStorage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pokemon);
requestQueue = Volley.newRequestQueue(getApplicationContext());
url = getIntent().getStringExtra("url");
nameTextView = findViewById(R.id.pokemon_name);
numberTextView = findViewById(R.id.pokemon_number);
type1TextView = findViewById(R.id.pokemon_type1);
type2TextView = findViewById(R.id.pokemon_type2);
pokemonName = getIntent().getStringExtra("name");
catchStorage = getSharedPreferences("catchStorage", Context.MODE_PRIVATE);
Button catchBut = (Button)findViewById(R.id.button);
if(catchStorage.getBoolean(pokemonName, false)){
catchBut.setText("Release");
}
else {
catchBut.setText("Catch");
}
load();
}
public void toggleCatch(View view) {
Button catched = (Button)view;
SharedPreferences.Editor editor = catchStorage.edit();
if(catchStorage.getBoolean(pokemonName, false)){
editor.putBoolean(pokemonName, false);
editor.commit();
catched.setText("Catch");
}
else {
editor.putBoolean(pokemonName, true);
editor.commit();
catched.setText("Release");
}
}