Problem in making TextView display value updated

1

I'm doing a simple application, it's just a counter, with two buttons, "+" and "-", which consists of displaying an initial value of 0, and if I click on any of the buttons, the value displayed on the screen will be increased, or decremented, according to the corresponding button. My problem is with displaying the new value on the screen. I get the value of the TextView, I make the changes, and finally, I can not display it on the screen, the app simply 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 layout components are still in disuse because I need to first make this code work, it is the main part.

Thank you very much for your help and attention right away.

    
asked by anonymous 28.07.2017 / 05:18

1 answer

3

First you have to declare the variable numero globally out of the button by initializing 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++;
        }
    });
}

Then you do not enter an integer value directly in the setText() method. Use the static method valueOf to convert first to string . Here's 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 toString() method. See this question what's the difference between using Object.toString () and String.valueOf () ?

    
28.07.2017 / 05:30