0

I'm developing an android application in which I have a thread that continuously generates data to be appended in a Textview, I use an Handler for this but after 1 or 2 secs my app just freezes.

The code is something like:

private class UpdateTextRunnable implements Runnable
{
    private String mBuffer = "";

    public void addLine( String line ) {
        mBuffer += line + "\n";
    }

    @Override
    public void run(){
        mTextView.setText( mBuffer );
    }
}

and in the thread a method from a custom interface is called every 0.5/1 sec :

    public void onMyCustomEvent( String data )
    {
      // mUpdater is an instance of UpdateTextRunnable
      mUpdater.addLine( data );
      // mHandler is an instance of Handler inside the main activity
      mHandler.post( mUpdater );
    }

What am I doing wrong ? :) Thanks

Simone Margaritelli
  • 4,584
  • 10
  • 45
  • 70

1 Answers1

0

try StringBuilder instead of the String.

Example:

private class UpdateTextRunnable implements Runnable
{
    private StringBuilder mSB = "";

    public void addLine(String line) {
        mSB.append(line + "\n");
    }

    @Override
    public void run() {
        mTextView.setText(mSB.toString());
    }
}
renyi
  • 11
  • 1
  • StringBuilder is already a CharSequence btw, no need for the #toString(). – Jens Aug 28 '12 at 19:48
  • try this http://stackoverflow.com/questions/5188295/how-to-change-a-textview-every-second-in-android – renyi Aug 28 '12 at 19:59