Android update TextView with Handler

I am having problems with updating the TextView, I used the Handler method to pass the message to the UI. My application receives data(type integers) true io stream and shows in TextView.

My Activity class looks like this:

public class DeviceView extends Activity {
    TextView dataX;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device_view);

        dataX = (TextView) findViewById(R.id.datax);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                dataX.setText(String.valueOf(msg.arg1));
            }
        };

    }
}

I also have a separate class it extends Thread:

public class IOThread extends Thread {

    public void run() {
        byte[] buffer = new byte[1024];
        int data;
        while (true) {
            try {
                data = in.read(buffer);
                Message message= Message.obtain();
                message.arg1= data;
                DeviceView.handler.sendMessage(message);
            } catch (IOException ex) {
                break;
            }
        }
    }
}

Do I have to make a separate variable type String and point it to variable data and at last calling the count? Would that be enough to update TextView?

Answers


Can you try using an interface. Let the Activity implement it, pass it to the IOThread class. Once you get the result, pass the result to the Activity.

Interface named InterfaceData

public void getData(int data);
public class DeviceView extends Activity implements InterfaceData{

        TextView dataX;
        Handler handler;
        IOThread ioThread;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.device_view);
            handler = new Handler();
            ioThread = new IOThread(this);
            dataX = (TextView) findViewById(R.id.datax);

        }

        @Override
        public void getData(int data){
           handler.postDelayed(new Runnable(){
           public void run(){
             dataX.setText(data);
            };
          },100);
        }

    }


> Thread class


    public class IOThread extends Thread {

       InterfaceData interfaceData;
       public IOThread(InterfaceData interfaceData){
         this.interfaceData = interfaceData;
       }

        public void run() {
            byte[] buffer = new byte[1024];
            int data;
            while (true) {
                try {
                    data = in.read(buffer);
                   interfaceData.getData(data);
                } catch (IOException ex) {
                    break;
                }
            }
        }
    }

I have found my problem it was not the Handler issue. THe code i posted at the beginning is coorect. The problem lyis on the way i read the received bytes[] array from the InputStream. I have tested by sending an integer int numbers = (int) 2 and when print this receivd data in terminal in Android app, it receivs only 1, even if i send int 3 or 4, i stil receive 1.

So i preceiated your example code @dcanh121 , but my question is actualy how do i read properly the integers that the server sends?

public void run() {
byte[] buffer = new byte[1024];
int data;

while (true) {
    try {

        data = in.read(buffer);
                Log.d(TAG + data, "test");
        Message message = Message.obtain();
        message.arg1 = data;                
        Log.d(TAG + message.arg1, "test");
        DeviceView.handler.sendMessageDelayed(message, 100);

        } catch (IOException ex) {
        Log.e(TAG_IOThread, "disconnected", ex);
        break;
        }
    }
}

Need Your Help

How to change the mp3's volume (levels) in Processing?

java audio arduino processing

I want to make the simple sequencer with Processing and Arduino.