I have a MainActivity and an AsyncTask MyTask. They are separate classes. In MyTask, I want to update a TextView in MainActivity.
This seems easy if MyTask in a inner class of MainActivity because I can just call findViewById(). But now these two classes are separate.
So in my MainActivity I do this:
MyTask myTask=new MyTask();
Context context=this.getApplicationContext();
myTask.execute(context);
And my task is like this:
public class MyTask extends AsyncTask<Context, Void, Void>
{
TextView mText;
protected Void doInBackground(Context... params) {
Context context=params[0];
mText = (TextView) ((MainActivity)context).findViewById(R.id.text);
...
}
}
I know the “mText = (TextView) ((MainActivity)context).findViewById(R.id.text);” is wrong but I haven’t found the solution.
Any Hints
Thanks in Advance
You can do something like this. You can ignore the WeakReference if the task won’t be long living: