Andrea
Andrea

Reputation: 172

How to update TextView dynamically

I'm trying to create a small service that connects to a site does the operations and during the process updates the textview present. Nothing wrong until I enter a for loop, because the activity stops, it becomes completely white until when it does not end.

I would like to update the text step by step.

I created an async task, but it gives me the error ' failed to load window.

Below is my code, I state that I put all the permissions.

public class GameTask extends AsyncTask<String, Integer, Long> {

private ProgressBar progressBar;
private TextView text;

public GameTask(ProgressBar progressBar, TextView text) {
    this.progressBar = progressBar;
    this.text = text;
}

@Override
protected Long doInBackground(String... strings) {
    startGameThread(progressBar, text);
    return null;
}


private void startGameThread (final ProgressBar progressBar, final TextView text) {
    final Handler mHandler = new Handler();
    final Runnable mRunnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        progressBar.setProgress(0);
                        text.setText("Thread Iniziato...");
                        for (int i = 0; i <= 1000; i++) {
                            if (i == 0 || i % 10 == 0) {
                                text.setText("NUMBER " + i);
                            }
                        }
                        progressBar.setProgress(100);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            };
    new Thread(mRunnable).start();
}
}

Upvotes: 0

Views: 187

Answers (3)

Shiva Snape
Shiva Snape

Reputation: 544

As this Process is Running Inside AsyncTask, You need to Use runOnUiThread method to update the TextView. UI can only be Upated on MainThread.

     private void startGameThread () {
//        final Handler mHandler = new Handler();

                final Runnable mRunnable = new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i <= 1000; i++) {
                            if (i == 0 || i % 10 == 0) {
                                final int finalI = i;
                                activity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {

                                        textView.setText(String.valueOf(finalI));
                                    }
                                });

                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }
                };
                new Thread(mRunnable).start();



    }

Upvotes: 0

Aaron
Aaron

Reputation: 3894

You can take full advantage of AsyncTask to perform tasks in background and UI thread without a Handler:

public class GameTask extends AsyncTask<String, Integer, Long> {

    private ProgressBar progressBar;
    private TextView text;

    public GameTask(ProgressBar progressBar, TextView text) {
        this.progressBar = progressBar;
        this.text = text;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // UI thread
        text.setText("Thread Iniziato...");
    }

    @Override
    protected Long doInBackground(String... strings) {
        // Non-UI thread
        for (int i = 0; i <= 1000; i++) {
            if (i == 0 || i % 10 == 0) {
                publishProgress(i/100, i);
            } else {
                publishProgress(i/100);
            }
            try {
                Thread.sleep(100); // Backpressure if needed for your sample
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        // UI thread
        progressBar.setProgress(values[0]);
        if (values.length > 1) {
            text.setText("NUMBER " + values[1]);
        }
    }
}

Upvotes: 0

user4571931
user4571931

Reputation:

Try this method into AsyncTask

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        try {
            progressBar.setProgress(0);
            text.setText("Thread Iniziato...");
            for (int i = 0; i <= 1000; i++) {
                if (i == 0 || i % 10 == 0) {
                    text.setText("NUMBER " + i);
                }
            }
            progressBar.setProgress(100);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Upvotes: 2

Related Questions