对于循环读取无穷大

时间:2012-07-27 15:21:18

标签: android for-loop

我正在尝试制作一个简单的屏幕,向您显示您在第二天所赚取的金额,因此我尝试创建一个实时Feed类型textveiw,每秒以您赚取的金额更新,我的代码,当它在我的手机上运行它只读“无限”,当我试图添加1秒延迟它冻结在一起,这是我写的代码,我使用for循环因为我不知道更好如果有人有更好的方法来实现我想做的事,请告诉我..

    // 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();
        }
    }

提前感谢您的帮助!

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

这是一个经过编辑的版本,但我仍然遇到一个问题,它只是在屏幕上显示无穷大,我该如何解决这个问题?或者我如何使用计时器方法?

    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

}

2 个答案:

答案 0 :(得分:2)

更好的方法是使用TimerTimerTask

编辑:  以下是用户Timer和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);

希望这有帮助。

答案 1 :(得分:-1)

[1]你在这里做按位AND | (按位AND - &gt;&amp;)

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

[2]你的loc始终为0,因为你做了 loc = 0 ,for for loop loc永远不会更新

所以loc!= 7 总是正确