Hello. I have a problem. There is a variable int sitost = 25; I want to make it so that when I click on button1, the sitost variable adds + 10. I do it this way, but the value does not add:

  1. The code for the gameview.xml file:

    <TextView android:id="@+id/sitost" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="plus" android:text="Button" /> 
  2. GameView.java file code:

     public class GameView extends Activity { TextView SitostCat; int sitcoll = 25; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Скрываем строку состояния */ requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.gameview); SitostCat = (TextView) findViewById(R.id.sitost); SitostCat.setText("Сытость " + sitcoll); } public void plus(View view) { switch (view.getId()) { case R.id.button1: sitcoll = sitcoll + 10; break; } } 

When you click on the button, nothing happens. What's my mistake? I would be very grateful for the help!

  • How do you check the value of a variable? Is it possible that after changing a variable, you need to add a text change (to the TextView sitost )? - Dmi7ry
  • I check the value as it is, ie, by the line SitostCat = (TextView) findViewById (R.id.sitost); SitostCat.setText ("Satiety" + sitcoll); which is higher. or do you need something different? - iKey
  • slightly changed the code to: public void plus (View view) {switch (view.getId ()) {case R.id.button1: SitostCat = (TextView) findViewById (R.id.sitost); SitostCat.setText ("Satiety" + sitcoll); sitcoll = sitcoll + 10; break; }} and it worked. Is it right to use this method? - iKey
  • The comment did not fit, wrote the answer - Dmi7ry

2 answers 2

How do you check the value of a variable? Is it possible that after changing a variable, you need to add a text change (to the TextView sitost )?

Usually, a setter is set up for such variables, which sets the value of a variable, and can also automatically set a new value in TextView . After all, if you need to change a variable, for example, in five more places of the program, you will have to duplicate such code everywhere ... And if you later want to change the TextView to, for example, an image, then you will have to make quite a few identical corrections.

I have no experience with Java, because I don’t know if it is possible to set a setter and getter for a variable (for example, in D, I can define a class member as property and then with a simple assignment operation, like x = 5; or use, like int n = x; ;, the given code will be executed). But in general terms, the meaning is the same:

 int value = 0; void valueSet(int val) { value = val; // здесь же любые нужные вам действия, например SitostCat = (TextView) findViewById(R.id.sitost); SitostCat.setText("Сытость " + val); } int valueGet() { return value; } 

that is, any access to a variable goes through these methods, and not directly.

    The fact is that the code in the onCreate() method is executed only once - at the start of activation, respectively, what you have set there setText() will also be executed only at the start.
    To dynamically change the state of widgets, you need to use methods that react to current events, in your case it is a click on the button, you need to change the state of the widget there when the event occurred. On the other hand, there is no need to get a reference to the same object (widget) every time - this needs to be done once, when creating the activation, and saving the value in the class field for later use in any activation methods.
    In the code, all this writing is as follows:

      public class GameView extends Activity { TextView mSitostCat; int mSitcoll = 25; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game_view); mSitostCat = (TextView) findViewById(R.id.sitost); mSitostCat.setText("Сытость " + mSitcoll); } public void plus(View view) { switch (view.getId()) { case R.id.button1: mSitcoll += 10; mSitostCat.setText("Сытость " + mSitcoll); break; } } 

    PS: By convention, the design of the code in Java variable names to write with a small letter, the words separated by a capital letter. In addition, the class field names are usually started with the letter 'm'.
    Words in the names of .xml files and identifiers divide the '_' - get used to follow these rules immediately and the programmer life will be easier in the future. In the code above, I made corrections on how it should look.