Skip to content Skip to sidebar Skip to footer

How Access Parent Class View From Callback Function In Asynctask?

Here is my sample code. public class test extends Activity { private TextView tv; public class Blap extends AsyncTask{ private test parent;

Solution 1:

AsyncTask offers you two posibilites to update the UI :

If you want to update the UI in parallel with the task executed in doInBackground() (e.g. to update a ProgressBar), you'll have to call publishProgress() inside the doInBackground() method. Then you have to update the UI in the onProgressUpdate() method.

If you want to update the UI when the task is done, you have to do it in the onPostExecute() method.

/** This method runs on another thread than the UI thread */@Override
protected String doInBackground(String... _params) {
    for (int progressValue = 0; progressValue  < 100; progressValue++) {
        publishProgress(progressValue);
    }
}

/** This method runs on the UI thread */
@OverrideprotectedvoidonProgressUpdate(Integer... progressValue) {
    // TODO Update your ProgressBar here
}

/**
 * Called after doInBackground() method
 * This method runs on the UI thread
 */@Override
protected void onPostExecute(Boolean _result) {
   // TODO Update the UI thread with the final result
}

In your case, you should use an AsyncTask<String, String, String> because you need doInBackground() to return a String. Then you just have to treat the result in onPostExecute()

publicclasstestextendsActivity {
    privateTextView tv;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        //work...
    }

    publicvoidbuttonclick(View v){
        tv = newTextView(this);
        newBlap().execute(editText1.getText().toString(), editText2.getText().toString());
        setContentView(tv);
    }

    publicclassBlapextendsAsyncTask<String, String, String> {
        private test parent;

        @OverrideprotectedStringdoInBackground(String... options) {
            this.auth(options[0], options[1]);
            String result = "my result";
            return result;
        }

        publicauth(String login, String password){
            //process auth
        }

        @OverrideprotectedvoidonPostExecute(String _result) {
           tv.setText(_result);
        }
    }
}

Solution 2:

Simply change your method aresponse as below:

publicvoidaresponse(String rs){
    runOnUiThread(newRunnable() {
        @Overridepublicvoidrun() {
            tv.setText(rs);             
        }
    });
}

In this way when ever and from where ever you call this method, it will update your TextView on UI thread.

Moreover, you may also consider to call this method from AsyncTask's onPostExecute method which will run it without any problem, but then its your choice :)

Solution 3:

AsyncTask has some methods which are automatically called on the UI thread. Override onPostExecute() and call aresponse() from there. More information is here: http://developer.android.com/reference/android/os/AsyncTask.html

Post a Comment for "How Access Parent Class View From Callback Function In Asynctask?"