Android studio TextView值不会更新

问题描述 投票:0回答:1

我目前正在开发一个应用程序,该应用程序从一氧化碳传感器接收蓝牙数据,然后再显示给用户手机。我具有蓝牙功能,正在接收所需的数据,但是,当尝试向TextView元素显示变量时,该元素不会更新,只是保持不变。

屏幕上的按钮(在下面的xml中)将触发另一个模块以启动蓝牙通信,然后应显示在屏幕上。这是按钮的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" />

这是我当前为完成这项工作而设置的代码。

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

我尝试过的。我已经尝试过仅显示文字,例如

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

但是这也不会改变任何东西。如果您需要任何其他代码或信息,请告诉我。

java android android-studio android-layout textview
1个回答
0
投票

似乎您正在线程中运行代码,有时可能会影响用户界面的性能,请尝试使用ASYNC任务;如果失败,请尝试在使用scrollBy方法执行代码后滚动父视图。

© www.soinside.com 2019 - 2024. All rights reserved.