开发者

SyncTask in separate class, reach my views

开发者 https://www.devze.com 2023-01-23 17:51 出处:网络
I have Asynch in a separate class, and I n开发者_高级运维eed to change setText on some of the TextViews

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消