简体   繁体   中英

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.

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. Here is the xml for the button and the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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