Vcoder
Vcoder

Reputation: 124

Button click change textview

I want to make a simple counter program where I have a Textview and two buttons, one with + another with - whichever I press I get 1 incremented or decremented.

My problem is that I want the Textview to update a variable and not text. (By that I mean I get the app to work with .setText("counter:"+ var_counter) but doesn't work with .setText(var_counter).

var_counter is an int.

    final TextView tvContador = (TextView)findViewById(R.id.tvContador);
Button add = (Button)findViewById(R.id.bMais);
Button sub = (Button)findViewById(R.id.bMenos);
add.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           contador++;                                 
           tvContador.setText(getText(contador));
       }
});

p.s: my string is a resource string.

Upvotes: 1

Views: 361

Answers (3)

Nolesh
Nolesh

Reputation: 7018

Simply:

.setText(""+var_counter);

Upvotes: 0

confucius
confucius

Reputation: 13327

use

.setText(Integer.toString(var_counter));

Upvotes: 0

K-ballo
K-ballo

Reputation: 81349

Since var_counter is an int, setText(var_counter) tries to set the text of the widget to the string resource with such value as its id. What you want is:

setText(Integer.toString(var_counter));

Upvotes: 1

Related Questions