0

I am trying to make a simple screen that shows you the amount of money you have earned by the second, so im trying to create a live feed type textveiw that updates every second with the amount of money you have earned, with my code when it runs on my phone it only reads "infinity" and when i tried to add in a 1 second delay it froze all together, here is the code that i wrote, i am using a for loop because i didnt know of a better way if anyone has a better way of achieving what i am trying to do please let me know..

    // Calculate pay per second
    double PPS = (HW/3600);
    double OTPPS = (OTW/3600);
    double HPDPS = (HPD*3600);
    double money = 0;
    double Reserve = 0;
    loc = 0;
    // Display
    for(int i=0; i<HPDPS & loc!=7; i++)
    {
        money = (PPS+Reserve);
        Reserve = (Reserve+money);
        TextView textView = (TextView) this.findViewById(R.id.yourpay);
        textView.setText(String.valueOf(money));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Thanks in advance for any help!

======================================================================================

Here is an edited version but i am still having a problem where it just displays infinity on the screen, how do i fix this? or how do i use the timer method?

    public void sendMessage(View view) {
    // Receive messages from options page
    Intent intent = getIntent();
    double HW = intent.getDoubleExtra(Options.MESSAGE_HW, 0);
    double OTW = intent.getDoubleExtra(Options.MESSAGE_OTW, 0);
    double HPD = intent.getDoubleExtra(Options.MESSAGE_HPD, 0);
    // Calculate pay per second
    double PPS = (HW/3600);
    double OTPPS = (OTW/3600);
    double HPDPS = (HPD*3600);
    double money = 0;
    double Reserve = 0;
    // Display
    for(int i=0; i<HPDPS; i++)
    {
        money = (PPS+Reserve);
        Reserve = (Reserve+money);
        TextView textView = (TextView) this.findViewById(R.id.yourpay);
        textView.setText(String.valueOf(money));
    }


    // set textView

}
Mr. Pivirotto
  • 281
  • 2
  • 6
  • 23

2 Answers2

2

Better way is to user a Timer and TimerTask.

EDIT: Here is a sample application code to user Timer and TimerTask:

final TextView t1 = (TextView) findViewById(R.id.textView1);

    final Timer t =new Timer();
    t.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    t1.setText("Hello" + counter++); //Place your text data here

                    //Place your stopping condition over here. Its important to have a stopping condition or it will go in an infinite loop. 
                    if(counter == 10)
                        t.cancel(); 
                }
            }); 
        }
    }, 1000, 1000);

Hope this helps.

Arun George
  • 18,352
  • 4
  • 28
  • 28
  • how would i go about doing this? ive never used the timertask or timer – Mr. Pivirotto Jul 27 '12 at 15:35
  • I always test the code before pasting it as a solution. This sample code is also tested and its working. Maybe the way you declared the `counter` variable is wrong. Declare `counter` as a `global` variable and it will increment from 0-9 at an interval of 1 second. – Arun George Jul 28 '12 at 06:43
  • sorry for the late response but i was out of town for the weekend, i am about to test this but it keeps telling me that i dont need the first @overide? its gives me an error on run() until i remove it.... could i be putting this code in the wrong place? i am putting it inside a "public void sendMessage(View view) {}" on my activty.java file – Mr. Pivirotto Jul 30 '12 at 17:15
-1

[1] You are doing Bitwise ANDing (Bitwise ANDing -> &) here

for(int i=0; i<HPDPS & loc!=7; i++)
                     ^

[2] Your loc in always 0, because you did loc=0 and in for loop loc is never updated

So loc!=7 is ALWAYS TRUE

Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
  • Your [1] is wrong: `&` is also a boolean operator. It is a bitwise operator when used between numbers, but here the `<` and `!=` have higher precendence so the expression is equivalent to `(i – assylias Jul 27 '12 at 15:29
  • See for example [this question](http://stackoverflow.com/questions/11411907/what-are-the-cases-in-which-it-is-better-to-use-unconditional-and-instead-of/11412037#11412037). – assylias Jul 27 '12 at 15:34
  • No @assylias, **&** is Bitwise ANDing, && is Operator for condition. – Chintan Raghwani Jul 27 '12 at 15:34
  • loc is declared before the for loop and gets set to 7 when my stop button is pressed – Mr. Pivirotto Jul 27 '12 at 15:35
  • What Nonse... @assylias, it is Bitwise ANDing. Try it as following: int i = 1; if (i == 0 & ++i != 2) { System.out.println("i"+i); } System.out.println(i); //2 i = 1; if (i == 0 && ++i != 2) { System.out.println("i"+i); } System.out.println(i); //1 – Chintan Raghwani Jul 27 '12 at 15:37
  • 1
    @ChintanRaghwani Whatever you call it, I'm saying that `(i – assylias Jul 27 '12 at 15:47