soricellia
soricellia

Reputation: 31

updating UI thread while in asynctask

as the title states, im trying to update something in my UI thread while running an asynctask.. i've read quite a bit on asynctask and it seems i should be able to change a variable from the onPostExecute() method. obviously this is not the case.

Here is my sample code:

TextView tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated); 
Login login = new Login();
login.execute(userName, password);

and here the the login class

public class Login extends AsyncTask<String, void, String>{
    public String doInBackground(String... params){
       logMeIn(params[0], params[1]);
    }
    public void onPostExecute(String update){
       tv.setText(result); //this is not working!!
    }

whats actually happening is tv is underlined red and eclipse says i need to create a local variable.. but i thought the onPostExecute is ran from the UI thread? confused :?
im trying to do what i found at this website. I'm not entirly sure what i'm doing and i would love a point in the right direction! thanks in advance.

Upvotes: 1

Views: 169

Answers (4)

Rudi Kershaw
Rudi Kershaw

Reputation: 12952

To bring your TextView instance into scope, pass your tv variable into your Login instance constructor and add the constructor and variable in as below.

public class Login extends AsyncTask<String, void, String>{

    private TextView tv;

    public Login(TextView tv){
       this.tv = tv;
    }
    public String doInBackground(String... params){
       logMeIn(params[0], params[1]);
    }
    public void onPostExecute(String update){
       tv.setText(result); //this should work
    }
}

You have to pass the variable to this instance, otherwise it is out of scope. You won't be able to access it unless you declare and instantiate a new variable within the Login class.

Upvotes: 0

ucsunil
ucsunil

Reputation: 7494

This is because the TextView variable tv is defined in another class that does not have Login as an inner class. If Login is an inner class of the class containing the variable tv, it should be able to see the variable tv.

An easy way to solve this would be to make your AsyncTask an inner class of your main class.

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

This line here

TextView tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated);

is obviously declared either

  1. Outside of a method -which would result in tv being null or
  2. In a method that isn't part of the AsyncTask- which would mean the task doesn't have access to it

You should define it as a member variable (outside of a method)

TextView tv;

then initialize it inside of a method

tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated);

This will give your task access to it as well as the rest of your class.

If your AsyncTask is a separate file than your Activity then you will want to see this answer on using an interface and create a callback to update the TextView in your Activity.

Upvotes: 1

BVB
BVB

Reputation: 5420

Place TextView tv; into the scope of the class and outside of the function. Leave tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated); where it is. You should then be able to access tv from onPostExecute().

Upvotes: 0

Related Questions