2

I'm facing a problem,On a button press i try to read the DataInputstream which has data coming in and display the data.

I'm using a while loop to read the data. But the dynamic update of the Textview doesnt happen.

TextView datatextview = (TextView)findViewById(R.id.data); 

DataInputStream Din = new DataInputStream(socket.getInputStream());
Button getData= (Button)findViewById(R.id.getdata);
getData.setOnClickListener(new OnClickListener() {
    public void onClick(View v) { 
    //.......stuff .......
    try{
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];
    int bytesRead = -1;
    String message1 = "";
    while (true) {
        message1 = "";
        data = Din.readLine();
        bytesRead = (reading).length();
        if (bytesRead != -1) {
            Log.v(TAG,"data"+data); //I'm getting the data correctly
            //But not able to update it in the TextView :(
            datatextview.setText(data);  //doesnt work
        }
    }
Will Tate
  • 33,439
  • 9
  • 77
  • 71
m4n07
  • 2,267
  • 12
  • 50
  • 65

2 Answers2

4

You have to exit your method and relinquish control of the UI thread in order for your views to update. You should probably be using AsyncTask with its progress updating functionality to do this, so that you're not hogging the UI thread.

Something like:

public class MyTask extends AsyncTask<Void, String, Void> {
  private final TextView progress;

  public MyTask(TextView progress) {
    this.progress = progress;
  }

  @Override
  protected void onPreExecute() {
    progress.setText("Starting...");
  }

  @Override
  protected Void doInBackground(Void... unused) {
    ... your buffer code, including publishProgress(data); in your while loop ...
  }

  @Override    
  protected void onProgressUpdate(String... data) {
    progress.setText(data[0]);
    progress.invalidate(); // not sure if this is necessary or not
  }

  @Override
  protected void onPostExecute(Void unused) {
    progress.setText("Finished!");
  }
}

Then you can create and execute your AsyncTask using:

new MyTask(datatextview).execute();

Edit - make sure to use @Override which helps alert you if your method signatures are not correct.

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Thank you Matthew. I will try and let you know. – m4n07 Mar 16 '11 at 16:26
  • I have my DataStream objects in a different class , and say myTask is a new class , now i hav the problem of accessing those. – m4n07 Mar 16 '11 at 17:05
  • you can transfer the data objects in onPostExecute, or you can use the AsyncTask as an inner class. – Matthew Mar 16 '11 at 17:09
  • I tried the same , by having my reading stream data in "doInBackground". But the result i got is "starting" . I was continuously getting data in the logcat. But not on the UI(textView). Then once i stopped the device i got finished in textView. I think the problem is that i'm invoking it from the class like Class A { In OnCreate() onButtonClick call-> new GetData(readingtextview).execute(); Class nestedB extends Async{ doInBackground() publishProgress() } ` } – m4n07 Mar 16 '11 at 18:17
  • I was trying to say that i'm using the nested class approach,but it didnt work . I'm just getting "Starting" in my TextView – m4n07 Mar 16 '11 at 18:21
  • What if you include @Override on your methods to make sure you are actually overriding them? – Matthew Mar 16 '11 at 18:49
  • The method should've been `onProgressUpdate`. I gave you the wrong name earlier. Using @Override would've flagged this as an error. Your code should work with the new version. – Matthew Mar 16 '11 at 18:54
  • Its working great !!! Thank you Matthew. I changed the name to onProgressUpdate and it works without overriding. – m4n07 Mar 17 '11 at 04:55
  • Great. You really should use @Override though. It doesn't change the program, but it does catch your spelling mistakes (like the one I made!) – Matthew Mar 17 '11 at 05:11
  • Hi Matthew, Is it possible to update to a canvas instead of updating to a textview. I was trying something like , but its not working . Any ideas . http://stackoverflow.com/questions/5635434/android-text-not-visible-on-writing-using-drawtext – m4n07 Apr 13 '11 at 05:47
0

Matthew is right here but to elaborate...

If you are doing this from a thread you are probably stuck looping so fast the UI never gets a chance to redraw with your new values. If you want to force a redraw of the UI from a thread call View.invalidate().

If you are doing this from your main thread (which you should definitely reconsider) again you are stuck looping and UI can't redraw...you would want to all View.postInvalidate() to force a UI redraw.

Will Tate
  • 33,439
  • 9
  • 77
  • 71