Settext causing text to flicker irratically

不羁的心 提交于 2019-12-12 02:36:43

问题


I'm making an app which establishes USB communication between an Arduino UNO R3 and an android tablet. Arduino board is sending data correctly and it is even being received by tablet correctly and when tried to display, the text does get printed but with a rather continuous flicker.

class MyThread extends Thread
{
    @Override
    public void run()
    {
        mCallback = new UsbSerialInterface.UsbReadCallback()
        { //Defining a Callback which triggers whenever data is read.
            @Override
            public void onReceivedData(byte[] arg0) //data received in bytes
            {
                String data = null;

                try
                {

                    data = new String(arg0, "UTF-8");
                    handler.post(new newthread(data));                            

                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }
            }
        };
    }
}
class newthread implements Runnable
{
    String str1;        

    public newthread(String STR1)
    {            
        str1 = STR1;
    }
    @Override
    public void run()
    {

        DoseRateDisplay = (TextView) findViewById(R.id.DoseRateDisplay);
        if(str1.contains("L"))
        { tv6.append("Health OK"); }
        else
        {
           DoseRateDisplay.settext(str1);
        }
    }
}

I think the reason for flicker can be that the data is incoming too fast. Using Thread.sleep does not help. What can be the possible solution to this problem ? Also, using append instead of settext doesn't cause any flickering problems, but then data gets appended to textview.


回答1:


From my comment: try to check if the text received and the text already in the TextView are equal:

if(!DoseRateDisplay.getText().toString().equals(str1)) {
    DoseRateDisplay.settext(str1);
}


来源:https://stackoverflow.com/questions/38203558/settext-causing-text-to-flicker-irratically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!