Jermaine C
Jermaine C

Reputation: 23

async task not updating screen

I have a method that is executed using asyncTask with a progressdialog. After this method is executed, it changes some things on screen - a spinner and a textview. Inside my method I update them with new data. I know that the method is running perfectly and is doing as it should, because when I press the arrow on the spinner the new data is inside it. Inside the method a setText() and notifyDataSetChanged() are called - these don't seem to update the screen. Adding notifyDataSetChanged() to onPostExecute() of my async task doesn't seem to do anything either.

Any ideas what is going wrong?

This is my code

myMethod()

aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
                        for(Object tmpObject:objects){
                            aa.add(tmpObject.getName());
                        }
                        spin=(Spinner)findViewById(R.id.spinner);
                        spin.setOnItemSelectedListener(this);
                        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin.setAdapter(aa);
                        aa.notifyDataSetChanged();
                        txt=(TextView)findViewById(R.id.txt);
                        txt.setText("Woohoo!");

My asyncTask

class loginTask extends AsyncTask<Void, Void, Void> {
    protected void onPreExecute() {
           pd = ProgressDialog.show(login.this, "Logging you in...", "Please wait", true, false); 
          }

    @Override
     protected Void doInBackground(Void... unused) {
         myMethod();
        return null;
     }
        protected void onPostExecute(Void unused) {
         pd.dismiss();




     }
}

Why isnt the screen updating after the progressdialog is finished? It updates just fine if I dont use asyncTask. Adding notifyDataSetChanged() to the postexecute doesn't do anything either.

Upvotes: 2

Views: 2497

Answers (2)

Rubycon
Rubycon

Reputation: 18346

doInBackground - method will be execute on the background thread, so it must not attempt to interact with UI objects. You can not perform here txt.setText("Woohoo!"); etc.

Upvotes: 2

Brian Cooley
Brian Cooley

Reputation: 11662

The doInBackground method isn't on the UI thread, so if you want to do UI updates, you should do them in onProgressUpdate or onPostExecute.

Upvotes: 8

Related Questions