-2

Possible Duplicate:
how to update the textview variable for every 5 seconds

i have two tasks to do simultaneously using the same data file.One is to read the txt file and plot it in the chartview. The other task is to do calculations with the plotted data. My android app layout should have these calculated results in textview above the chartview and it should get updated constantly for 5 sec while the ploting is going on dynamically in the chart.

I have problem with the second task - updating the values in textview which runs in a while loop. I have used Handler,Asynctask,Thread, Wait. It works perfectly as java app.But, it dint work as an android app. My app either force closes or takes the delay before startup (i.e.) the app opens only after the delay given and shows the last value of while loop execution. I also did this in background, but it affects the plotting.

   while(p[y1]!=0)
 {
    rr = ((p[y1]-p[y1-1]));
    double x = rr/500;
    HR = (int) (60/x);                         
     final Handler handler=new Handler();
      handler.post(runnable);
          final Runnable runnable=new Runnable()
          {
          @Override
          public void run()
          {                      
                  hr.setText(String.valueOf(HR));
                handler.postDelayed(runnable, 5000);
          }
          };

          y1++;
            }

I want the hr value to be updated every 5 sec, without affecting the chart. anyone pls help. Thanks in advance for the help.

Community
  • 1
  • 1
Pooja
  • 77
  • 1
  • 6

1 Answers1

0

have you tried something like, create a inner thread class, in its run method do what you need. you can also try sleep for 5 sec in its run. make sure your while loop is inside the run method.

i below code you have thread inside a while loop, but you should have loop inside a thread.let me know if u face any issue doing this.

boolean isAlive = true; //change it  to falst whenever u want to stop the thread
class localT extends Thread {

@Override
public void run() {
    while(isAlive) {

        try {
            sleep(5000);

            textView.setText("yourChanging text");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    super.run();
};

}

use something like localThread.start(); , kill// stop this thread on on pause, on destroy.

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • I want to increment the variable y1 for while loop checking as well as for calculation.. so, this method has no option to increment it. Should i do in some other way?? – Pooja Jan 24 '13 at 09:13
  • you can cheange while condition. it is just an dummy code i have written for u to understand how to do it via thread – AAnkit Jan 24 '13 at 09:20
  • Can you pls suggest me some solution. – Pooja Jan 24 '13 at 09:29
  • IN while condition- while(p[y1]!=0].... to check this condition for whole data i need to increment it. but it shows error as y1 cannot be assigned in a enclosing type. what should i do here. – Pooja Jan 24 '13 at 09:33
  • @Ankit- i want to update the hr value until p[y1] holds some value(txt file data). when i do this in for loop my app force closes. So, i checked the condition with while loop, but it shows only the last value of the loop. Wat am I suupposed to do to get it right? – Pooja Jan 24 '13 at 09:53