5

I'm trying to make a basic counter.

The idea is that the user presses the button and the app displays how many times the button has been pressed.

My problem is that I am unsure of how to update the text view. My understanding is that the XML parts of it retrieves the strings, which are set in stone upon runtime. So how am I supposed to "update" something that is "final"?

My understanding is that When the button is pressed, I increment num by 1. Then, it gets the prompt string (Clicks: %d) and displays it to the screen. However, whenever I run this, it just crashes.

public class HelloAndroid extends Activity{
/** Called when the activity is first created. */

int num = 0;
TextView tView;
Button clickhere;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tView = (TextView) findViewById(R.id.textView1);
    clickhere = (Button) findViewById(R.id.button1);

    clickhere.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            num++;
            String display = String.format(getString(R.string.prompt), num);
            tView.setText(display);
            setContentView(tView);

        }
    });

} 
}

Any help would be appreciated.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
RTL
  • 53
  • 1
  • 1
  • 4
  • Just a quick addition tpo the previous answers: You don't need to call setContentView(tView); in the onClick() event. – Aleadam Apr 14 '11 at 01:58
  • Thank you very much, Aleadam! That was just what I needed to get it to work! – RTL Apr 14 '11 at 02:02
  • glad you got it working, although the main work was done by Brian and corey. +1 for them. I also see it's your first question here, so kudos to you for a well-formatted question that demonstrates the effort put before asking. +1 for you also :) – Aleadam Apr 14 '11 at 02:06
  • 3
    Possible duplicate of [How to change a TextView's text by pressing a Button](http://stackoverflow.com/questions/6716748/how-to-change-a-textviews-text-by-pressing-a-button) – Ciro Santilli OurBigBook.com Feb 03 '16 at 14:40

2 Answers2

4

You are setting the TextView when it is clicked (not when the button is clicked), I am guessing that is not what you meant to do. I have update your code below to set the TextView when the button is clicked.

 public class HelloAndroid extends Activity{
/** Called when the activity is first created. */

int num = 0;
TextView tView;
Button clickhere;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tView = (TextView) findViewById(R.id.textView1);
    clickhere = (Button) findViewById(R.id.button1);

    clickhere.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String display = String.format(getString(R.string.prompt, Integer.toString(++num)));
            tView.setText(display);
        }
    });

    //you don't need an event handler on the TextView (given the description of the problem)

} 
}
Brian ONeil
  • 4,229
  • 2
  • 23
  • 25
  • Thanks, I realized this just before you replied and I fixed that portion, but I still crash every time I try and click. – RTL Apr 14 '11 at 01:48
2
clickhere.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        num++;
        tView.setText(Integer.toString(num));
    }
});

this will set your textview equal to num

corey
  • 498
  • 3
  • 10