tags:

views:

56

answers:

2

In c it would be: while(1){ i++; printf("%d \r",i); } I assume the textview and the variable and the timer get created in oncreate, then there is a timer handler with an increment and a settext and a sleep? Seeing how to do this in androidese would really clarify things I think. Thanks.

+1  A: 

This could be done with a timer... something like this:

mTextView = (TextView)findViewById(R.id.text);

Timer timer = new Timer();
IncrementTask task = new IncrementTask(mTextView);
timer.scheduleAtFixedRate(task, 0, 1000);

.... and a subclass of TimerTask:

class IncrementTask extends TimerTask {
    WeakReference<TextView> mRef;
    int counter = 0;
    Handler handler = new Handler();

    public IncrementTask(TextView text) {
        mRef = new WeakReference<TextView>(text);
    }

    public void run() {
        handler.post(new Runnable() {
            public void run() {
                mRef.get().setText("counter " + counter);
                counter++;
            }
        });
    }
}
synic
It works perfect :D up vote!
Cristian
A: 

The foreground task doesnt time out unles you starve it for 4 seconds. Surely one could increment a variable and update a textview in a 500mhz processor in microseconds. I think its inconceivable to have to spawn a task to increment a variable. Its not a cpu intensive task. I was hoping to see a small elegant and understandable solution. The only part of java that I'm understanding is that its just like c but no pointers... I aint getting a lot of the subtle nuances yet...

bobgardner