I have Asynch in a separate class, and I n开发者_高级运维eed to change setText on some of the TextViews
How this possible? OR should I keep AsyncTask inside my Class?
private class DownloadImageTask extends AsyncTask<Object, Void, AdModel> {
@Override
protected AdModel doInBackground(Object... params) {
return getAd();
}
protected void onPostExecute(AdModel result) {
textTitle.setText(result.getTitle());
}
}
You could create a constructor for your AsyncTask that takes a reference to your activity. You might want to be careful about not leaking your activity reference by nulling it out from within your AsyncTask when it's done.
private class DownloadImageTask extends AsyncTask {
private TextView text;
DownloadImageTask(TextView txtToUpdate) {
text = txtToUpdate;
}
@Override
protected AdModel doInBackground(Object... params) {
return getAd();
}
protected void onPostExecute(AdModel result) {
text.setText(result.getTitle());
text = null;
}
}
or ..better... create listener interface, which You will invoke at onPostExecute. Implementation of listener interface should update TextView.
精彩评论