简体   繁体   English

Android studio TextView 值不会更新

[英]Android studio TextView value won't update

I'm currently working on an app that receives bluetooth data from a Carbon monoxide sensor before displaying it to the users phone.我目前正在开发一个应用程序,该应用程序从一氧化碳传感器接收蓝牙数据,然后将其显示给用户手机。 I have the bluetooth side of things working and I'm receiving the data I require however, when trying to display the variable to the TextView element, the element doesn't update it just stays the same.我有蓝牙方面的工作,我正在接收我需要的数据但是,当试图将变量显示到 TextView 元素时,元素不会更新它只是保持不变。

The button on the screen (in the below xml) will fire off another module to start the bluetooth communications and should then display to the screen.屏幕上的按钮(在下面的 xml 中)将触发另一个模块以启动蓝牙通信,然后应该显示在屏幕上。 Here is the xml for the button and the TextView这是按钮的 xml 和 TextView

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="350dp"
    android:layout_marginBottom="160dp"
    android:gravity="center"
    **android:onClick="onStart"**
    android:text="Start"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<TextView
    android:id="@+id/textViewCO"
    android:layout_width="53dp"
    android:layout_height="14dp"
    android:layout_marginEnd="350dp"
    android:layout_marginBottom="408dp"
    **android:text="Test"**
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

Here is the code I currently have set up for trying to do the job.这是我目前为尝试完成这项工作而设置的代码。

            while (btSocket.isConnected()) {

            //reading the bluetooth input
            info = inputStream.read(buffer);
            //assigning read value from the buffer to a string 
            String readMessage = new String(buffer, 0, info);

            //New textview object
            TextView tv = (TextView) findViewById(R.id.textViewCO);
            //Display variable
            tv.setText(readMessage);
            //Console log
            System.out.println(readMessage);


            try {
                Thread.sleep(3200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

What I have tried.我已经尝试过。 I have tried simply displaying text eg我试过简单地显示文本,例如

tv.setText("This is some text");

However this also doesn't change anything.然而,这也不会改变任何事情。 If you need any other code or information please do let me know.如果您需要任何其他代码或信息,请告诉我。

seems you are running your code in a thread, sometimes that can affect the perfomance of the user interface, try using ASYNC tasks and if that faills, try to scroll the parent view after executing your code using the scrollBy method.似乎您正在线程中运行代码,有时这会影响用户界面的性能,请尝试使用 ASYNC 任务,如果失败,请在使用 scrollBy 方法执行代码后尝试滚动父视图。

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

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