1

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?

jnthnjns
  • 8,962
  • 4
  • 42
  • 65
yadabadu
  • 19
  • 1
  • 6
  • Your code looks ok. does it gives you an error message? – La bla bla Jul 25 '12 at 21:51
  • Can you verify that you are receiving messages through the Handler? – telkins Jul 25 '12 at 22:11
  • Yes, i can send and receive the data, and receiving data from handler works too, it sets data to the textView only the last receivd data (integer). The server side application sends randome numbers from 1 to 20 true I/O, and at this moment my android application prints only last number 20. So my question is where do i have to call method handler.postDelay(this, 1000); to delay the quea and would that be enoughf? is that the solution? Or do i have to do more then that? – yadabadu Jul 25 '12 at 23:00

2 Answers2

0

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;
                }
            }
        }
    }
dcanh121
  • 4,665
  • 11
  • 37
  • 84
  • I can try but that does not answers my question and it confuesing me too :( . Why would i make an Interface if i already used Handler to pas the data to the UI???? – yadabadu Jul 26 '12 at 10:29
  • You used the Handler as static. Is there a problem with that? – dcanh121 Jul 26 '12 at 17:42
  • I tryd your way, except i changed the code a little bit. Becouse the server sends integers(1,2,3..) and not String(text). So the method on interface i changed to public void getData(final int data); But i get an nullpointerexception at interfaceData.getData(data); and no i dont have problems with handler beeing static. @dcanh121 – yadabadu Jul 26 '12 at 20:47
  • I didn't see you creating the IOThread class. I assumed that you will pass the Activity instance to that class to initialize the constructor. Anyways, I edited the code in activity class. – dcanh121 Jul 26 '12 at 23:16
  • I also have two parameters in IOThread class constructor. `public class IOThread extends Thread { private static final String TAG_IOThread = "IOThread"; public final BluetoothSocket bSocket; private final InputStream in; private finalOutputStream out; InterfaceData pData; public IOThread(BluetoothSocket socket, InterfaceData pData) { this.pData = pData; bSocket = socket; }` @dcanh121 – yadabadu Jul 27 '12 at 10:02
  • I also noticed in the method public void getData(); in the Activity class, you do `dataX.setText(data)`. Should't that be a `dataX.setText(String.valueOf(data));` ?? @dcanh121 – yadabadu Jul 27 '12 at 10:27
  • It depends on what data you want to pass from IOThread to the Activity class. I only wrote a sample code. – dcanh121 Jul 27 '12 at 17:00
0

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;
        }
    }
}
yadabadu
  • 19
  • 1
  • 6