简体   繁体   English

Android TextView.setText有时仅在Runnable内部工作

[英]Android TextView.setText working only sometimes inside Runnable

I have a postDelayed Runnable that counts down from 3 the executes some code x number of times. 我有一个postDelayed Runnable,它从3倒数执行一些代码x次。 I am displaying text to the user via setText(). 我正在通过setText()向用户显示文本。 The setText() function works in some areas of an if,else if,else but does not work in other areas. setText()函数在if,else if,else的某些区域中起作用,但在其他区域中不起作用。

    final Runnable r1 = new Runnable() {
        @Override
        public void run() {
            if ((--count != 0) && (gestureCounter != selectedItems.size())) {
                mHandler.postDelayed(this, 1000);
                liveView.setText("Do " + selectedItems.get(gestureCounter) + " in " + String.valueOf(count));//works
            }
            else if(gestureCounter != selectedItems.size()){
                count=4;//3 seconds + 1
                liveView.setText("Hold " + selectedItems.get(gestureCounter));//NOT WORKING
                mHandler.post(this);
                fcalc.setTrain(true);
                while(fcalc.getTrain()){
                    //wait till trainig is done
                }
                gestureCounter++;
            }
            else{
                liveView.setText("");//works
                fcalc.Train();
                fcalc.setClassify(true);
            }
        }
    };
    mHandler.post(r1);

Please take a look at my comments to see which setText() functions work or don't. 请看一下我的评论,看看哪些setText()函数起作用或不起作用。 The one in the else if is the only one that is not working. else if中的一个是唯一不起作用的。

Any ideas? 有任何想法吗?

Thank you for your help! 谢谢您的帮助!

This is because of you are calling mHandler.post(this); 这是因为您正在调用mHandler.post(this); this will call immediately the handler runnable again and also you are incrementing the value gestureCounter , so next time will come to this loop and it will go to first loop or 3rd loop as per condition 这将立即再次调用可运行的处理程序,并且您还将增加gestureCounter的值,因此下一次将进入此循环,并将根据条件进入第一个循环或第三个循环

Try to add delay in that loop also and check 尝试在该循环中添加延迟并检查

Edited: 编辑:

Because of your while loop continuously running, UI is not updating, Just comment that and run the code like below condition it will work fine 由于您的while循环持续运行,UI并未更新,只需注释并按以下条件运行代码即可正常工作

like this 像这样

                    else if(gestureCounter != selectedItems.size()){
                    liveView.setText("Hold " +
                            selectedItems.get(gestureCounter));//NOT WORKING

                    mHandler.postDelayed(this, 1000); // delay added 

                    if(fcalc.getTrain()) {
                        return;
                    }
                    count=4;//3 seconds + 1

//                    fcalc.setTrain(true);
//                    while(fcalc.getTrain()){
//                        //wait till trainig is done
//                    }
                    gestureCounter++;
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM