A long text in the TextView
Asked Answered
H

4

2

I have a button, and when i click to it, i make textView.setText() a very big text, 50000 symbols, and the interface stops for 3 seconds, how can i fix it?

  1. I tried to make it with Handle and thread, but it hasn`t helped.
  2. I tried to make textview.append(), but it also hasn`t helped.

                MainActivity.this.runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
    
                    textText.append(rowItemHeader.getText().substring((endOfNews - 10000, endOfNews));
                }
            });
    

    Edit 1 no result

private class MyTask extends AsyncTask <Void, Void, String> { String str; TextView txt;

    MyTask(String str, TextView txt){
        this.str = str;
        this.txt = txt;

    }
    public String doInBackground(Void... args) {

        return str.substring(endOfNews - 10000, endOfNews);
    }

    public void onPostExecute(String myString) {
        // do your update here or you will get that error (the original thread one)
        txt.append(myString);
    }
}
Hortative answered 24/6, 2013 at 14:25 Comment(4)
What is not working? Is the variable myString correctly elaborated?Veld
its working, but i have delay for 1-3 seconds, when i click to button. If i have small text, it works fast, but if i have very larege text, i have delay.Hortative
Ok, but have you fixed this with the AsyncTask?Veld
No, everything is the same, i append 10000 symbols, and i think that setText() and append() works very slow with big text. So, I cant do it in background, i need to do it On Ui Thread :(Hortative
M
0

How did you declare the thread ? Yu've to use something like that :

this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Log.e("TAG synchronize", "synchronize");
        }
});
Margemargeaux answered 24/6, 2013 at 15:9 Comment(1)
I did like that, but it also stops interface for 1-3 seconds :(Hortative
B
0

Use AsyncTask, they are meant for exactly what you want. They are a secondary thread that is easy to setup and executes from your main thread.

Use a loop inside the thread that appends portions of the generated text to the TextView using runOnUiThread(new Runnable(...)...) and call Thread.sleep(50) which forces the Thread to sleep for that much milliseconds to prevent the lag.

Bordeaux answered 24/6, 2013 at 15:55 Comment(2)
If i do with AsyncTask i have error "Only the original thread that created a view hierarchy can touch its views."Hortative
Then you didn't do it fully, you didn't do the runOnUiThread() that makes it "run on the ui thread"! Only appending should be "run on the ui thread" don't put the entire AsyncTask operations to run on the ui as that would make it freeze as well.Bordeaux
V
0

Use an AyncTask with something like:

private class MyTask extends AsyncTask<Void, Void, String> {

    private Context context;       

    public MyTask(Context context) {
        this.context = context;
    }

    public String doInBackground(Void... args) {
        // do your job here
        return myString;
    }

    public void onPostExecute(String myString) {
        // do your update here or you will get that error (the original thread one)
        ((TextView)context.findViewById(R.id.textview)).setText(myString);
    }
}

and then in your activity

new MyTask(MyActivity.this).execute();
Veld answered 25/6, 2013 at 8:25 Comment(0)
T
0

First, remove the view, then add the text on background thread, then add the view back. Tell me if you need some sample code.

Toscano answered 25/6, 2013 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.