Reputation: 124
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
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