Problem in making Textview display updated value

Asked

Viewed 1,267 times

1

I’m making a simple application, it’s just a counter, with two buttons, "+"e"-", which consists of displaying an initial value of 0, and if I click any of the buttons, the value displayed on the screen will be added, or decremented, according to the corresponding button. My problem is in displaying the new value on the screen. I take the value of Textview, make the changes, and finally, I can’t display it on the screen, the app just closes.

 public class MainActivity extends AppCompatActivity {

Button btmais, btmenos, btmais2, btmenos2 ;
TextView pontuacao, pontuacao2;
EditText nome, nome2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btmais = (Button) findViewById(R.id.btmais);
    btmenos = (Button) findViewById(R.id.btmenos);
    btmais2 = (Button) findViewById(R.id.btmais2);
    btmenos2 = (Button) findViewById(R.id.btmenos2);

    pontuacao = (TextView) findViewById(R.id.pontuacao);
    pontuacao2 = (TextView) findViewById(R.id.pontuacao2);

    btmais.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            int numero = Integer.parseInt( pontuacao.getText().toString() );
            numero = numero + 1;
            pontuacao.setText(numero);
        }
    });
}

}

Xml codes:

<TableLayout
    android:layout_width="182dp"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/nome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />

    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/pontuacao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:textSize="100sp"
            tools:text="00" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/btmais"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="+" />

    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/btmenos"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="-" />

    </TableRow>

</TableLayout>

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/nome2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />

    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/pontuacao2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="01"
            android:textAlignment="center"
            android:textSize="100sp"
            tools:text="@string/pontuacao2" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/btmais2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="+" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btmenos2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="-" />
    </TableRow>

</TableLayout>

Many components of the layout are still in disuse because I first need to make this code work, it is the main part.

Thank you very much for your help and attention

Este é o layout

  • Edit the question and insert your activity_main.xml into it.

1 answer

3


First you have to declare the variable numero globally outside the Initiating button with 0.

private int numero = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btmais.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            numero++;
        }
    });
}

Ai then do not insert an integer value directly into the method setText(). Use the static method valueOf to convert first to string. See how it should look:

pontuacao.setText(String.valueOf(numero));

See the final result:

final int numero = 0;
btmais.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        numero++;
        pontuacao.setText(String.valueOf(numero));
    }
});

You can also use the method toString(). Look at this question what is the difference between using Object.toString() and String.valueOf()?

  • Already tried, continues the same way. I run it on mobile, and appears the notification "The score app stopped"

  • But then you declared a constant, you can’t change its value, and it needs to change. But it helped me a lot of way, thank you.

  • If when you click the button as it is in the reply and change the value of Textview, then it is not constant, but variable. The number++ increments 1 in the value you declared outside the button.

  • But android studio returns me "Cannot assign a value to final variable 'number' "

  • @Igor edited the question again, putting off onCreate without the 'final''...

  • @Igor what you can’t even do is put the opening variable inside the button, otherwise you can’t change in another case depending on the situation.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.