Callback Issue

+1 Eanna Wood · January 24, 2015
I am following the Android App Development for Beginners series and I am on number 19 and I have a button and and textbox. To start with the text box has 0 in it. I need it so that when I hit the button the number is increased by 1. I have got it to work using the "hardcoded" method of putting a "1" inside the settext brackets. Can I put a string in it or what do I need to do to get somekind of method working inside there.
Any help is greatly appreciated.

Post a Reply

Replies

Oldest  Newest  Rating
0 Itay Rabin · January 24, 2015
you can call a method that takes the number in the TextView, converts it to an integer ( Integer.parseInt(String) )
ups it by 1 and sets the TextView to that number
0 AKT Arun · January 24, 2015
For incrementing the number, first you can get the number from text box using getText() method. Since it returns a string value, you have to convert it into an integer using Integer.parseInt(). Then add 1 to that value and use setText() method to set the value of text box.
0 Eanna Wood · January 25, 2015
I'm a little confused as I'm very new to java.
Here is what I have
 Button add1 = (Button)findViewById(R.id.add1);

        add1.setOnClickListener(
       new Button.OnClickListener() {
             public void onClick  (View v) {
                 TextView Score1 = (TextView)findViewById (R.id.Score1);
                 getText(R.id.Score1);
                 //Integer.parseInt()
                // Score1.setText();

I'm not sure where I get the value from the getText(). I'm also not sure how to use that "Integer.parseInt()" Variable.
Thanks
0 AKT Arun · January 25, 2015

Button add1 = (Button)findViewById(R.id.add1);

add1.setOnClickListener( new Button.OnClickListener() {
public void onClick (View v) {
TextView Score1 = (TextView)findViewById (R.id.Score1);

// get score from text view
String value = Score1.getText().toString();

// convert it to integer so that we can increment it
int score=Integer.parseInt(value);

//increment score
score=score+1;

// now we set the new sore to the TextView using setText() method,
// String.valueOf() method converts our score into string
Score1.setText(String.valueOf(score));

0 Eanna Wood · January 25, 2015
Thank you very much!. That was the solution I needed. The app is now complete (its very simple) Thanks again
  • 1

Java / Android Development

106,932 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator