1

I am trying to update a textview inside a runOnUiThread or in onPostExecute in asynctask but it isn't updating.

In onCreate I am initializing all the textviews in a global variable _tvs.

_tvs.add((TextView) findViewById(R.id.maxCholTextView));
_tvs.add((TextView) findViewById(R.id.maxFatTextView));
_tvs.add((TextView) findViewById(R.id.maxSaturatedFatTextView));
_tvs.add((TextView) findViewById(R.id.maxFiberTextView));
_tvs.add((TextView) findViewById(R.id.maxSaltTextView));
_tvs.add((TextView) findViewById(R.id.maxCalTextView));
_tvs.add((TextView) findViewById(R.id.maxCarbTextView));
_tvs.add((TextView) findViewById(R.id.maxSugarTextView));
_tvs.add((TextView) findViewById(R.id.maxProteinTextView));

Then I tried three different ways to update it. In the first way I tried to call it from the doInBackGround function and set the text in runOnUiThread. In the second option I removed the runOnUiThread code and called updateView from onPostExecute. And the third option I put the settext outside the runOnUiThread code like you can see below. This works for initializing it, updating it when the activity gets started from another thread but crashes when the activity is resumed when the user presses the back button. The reason for the crash is that only the original thread that created a view hierarchy can touch its views. I don't have a clue why it doesn't complain when initializing it, updating it when another activity calls starts the activity but does when the activity gets resumed.

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(String... type) {
            // TODO Auto-generated method stub

                updateView();
            }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            //updateView();
        }

        private void updateView() {
            int progress;

            // proteins, carbs, sugars, fats, cals, cholesterol, salt
            if (_current != null && _diet != null && _diet.size() > 0 && _current.size() > 0) { 

                for (int i = 0; i < _pbs.size(); i++) {
                    final ProgressBar pb = _pbs.get(i);
                    final Float current = _current.get(i);
                    final TextView tv = _tvs.get(i);

                    Float diet = _diet.get(i);

                    progress = Math.round(((current / diet)) * 100);

                    int y = 50 + (int) (progress * 3.9); 

                    if (y > 400)
                        y = 400; // Cap is 400


                    // If I put this code inside the runOnUiThread the textView doesnt update
                    tv.setY(-y);
                    tv.setText("" + Math.round(current));


                    final int finalProgress = progress;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            pb.setMax(50);
                            pb.setProgress(20);
                            pb.setProgress(0);
                            pb.setMax(100);
                            pb.setProgress(finalProgress); // Force update -> Its an android bug thats not fixed yet http://stackoverflow.com/questions/4348032/android-progressbar-does-not-update-progress-view-drawable

                        }
                    });

                }
            } else if (_current == null || _current.size() == 0) {
                reInitPbs();
            }
        }

        private void reInitPbs() {
            for (int i = 0; i < _pbs.size(); i++) {
                final ProgressBar pb = _pbs.get(i);
                final TextView tv = _tvs.get(i);

                tv.setY(-50);
                tv.setText("" + 0);


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pb.setMax(50);
                        pb.setProgress(20);
                        pb.setProgress(0);
                        pb.setMax(100);
                        pb.setProgress(0); // Force update -> Its an android bug thats not fixed yet http://stackoverflow.com/questions/4348032/android-progressbar-does-not-update-progress-view-drawable

                    }
                });
            }
        }
    }

My activity onResume looks like this.

@Override
    public void onResume() {  // After a pause OR at startup
        super.onResume();

        if (_user != null) {
            ImageView avatarView = (ImageView) findViewById(R.id.avatarView);

            int id = getApplicationContext().getResources().getIdentifier(_user.getAvatar().getType().toString().concat(_user.getAvatar().getStatus().toString()).toLowerCase(), "drawable", getApplicationContext().getPackageName());

            avatarView.setImageResource(id);

            setFeedbackMessage();

            getDataFromDatabase("overview", "");
        }
    }

Any help would be greatly appreciated. I've been scrolling through questions and answers but still haven't found a solution to my problem. Thank you!

user3801533
  • 321
  • 1
  • 6
  • 15
  • you can use `onProgressUpdate` to update UI from AsyncTask `doInBackground`, This method will be run on Main(UI) thread by android. Reference: https://developer.android.com/reference/android/os/AsyncTask.html – Bipin Vayalu Apr 11 '17 at 12:49
  • @BipinVayalu I already tried that like I stated in the question. That is still not working. – user3801533 Apr 11 '17 at 14:35

0 Answers0