0

I want to generate a TextView inside AsyncTask's onPostExecute like this :

 protected  class AsyncTranslator extends AsyncTask<String, JSONObject, String>
    {
        @Override
        protected String doInBackground(String... params) {

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }

    @Override
    protected void onPostExecute(String mymeaning) {

            TextView myView  = new TextView(this);
            myView.setText(Html.fromHtml(myString));
    }
}

But it gives error telling me that this cannot be applied to AsyncTranslator. Can you tell me how I can generate textViews inside AsyncTask onPostExecute? Thanks.

jason
  • 6,962
  • 36
  • 117
  • 198

3 Answers3

3

From the documentation the possible constructors are

TextView(Context context)
TextView(Context context, AttributeSet attrs)
TextView(Context context, AttributeSet attrs, int defStyleAttr)
TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

but you are doing

TextView myView  = new TextView(this);

inside AsyncTranslator which is incorrect.

You can easily create a TextView inside your AsyncTask if you have a reference to your context. See this thread to get a reference to your context.

EDIT It seems that you already have a reference to your context, so just do

TextView myView  = new TextView(context);
Community
  • 1
  • 1
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
  • Thanks but now it says : **Cannot resolve symbol 'context'** – jason Nov 10 '15 at 08:34
  • But on `onPreExecute()` you do `Toast.makeText(context, "Please wait.", Toast.LENGTH_SHORT).show();`, so you should know `context` – ThomasThiebaud Nov 10 '15 at 08:35
  • Yes but that part is commented out in real code. So there is no context. How can I initialize a context quickly? Thanks. – jason Nov 10 '15 at 08:36
  • I put a link in my answer. See this [thread](http://stackoverflow.com/questions/16920942/getting-context-in-asynctask) to get a reference to your context. – ThomasThiebaud Nov 10 '15 at 08:37
  • inflate your view inside oncreate of your activity file or you can pass your activity context. – Srishti Roy Nov 10 '15 at 08:38
  • 1
    But I think you do not need to create your `textView` view here, just set the text. Your view must be created in your activity or fragment. – ThomasThiebaud Nov 10 '15 at 08:41
  • @Maloubobola I really need to generate textView in there and I couldn't manage to add context in my AsyncTask – jason Nov 10 '15 at 08:43
1

In AsyncTask you shouldn't make operations on base UI thread and here you are trying to do it. Try to create new Interface which lets you to pass the result.

public interface asyncTaskInterface {
    public void printEditText();
}

Then in your AsyncTask:

protected  class AsyncTranslator extends AsyncTask<String, JSONObject, String>
{
   public asyncTaskInterface delegate;

    @Override
    protected String doInBackground(String... params) {

}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    Toast.makeText(context, "Please wait.", Toast.LENGTH_SHORT).show();
}

@Override
protected void onPostExecute(String mymeaning) {

        delegate.printEditText();
}
}

In your result class you have to implement the interface and pass the class to your async task as delegate:

public class myClassActivity implements asyncTaskInterface ...

before you will call async task assign the delegate:

AsyncTranslator translator = new AsyncTranslator();
translator.delegate = this;
translator.execute();

At the end in your activity overwrite the method from your intrface and build in it the TextView.

Blady214
  • 729
  • 6
  • 19
0

First create an interface like this:

public interface onTextViewCreatedListener {
        public void onTextViewCreated(TextView tv);
    }

Then change your AsyncTranslator class like this

    protected  class AsyncTranslator extends AsyncTask<String, JSONObject, String>
        {

            private onTextViewCreatedListener onTextViewCreatedListener;

            public AsyncTranslator(onTextViewCreatedListener onTextViewCreatedListener){
                this.onTextViewCreatedListener = onTextViewCreatedListener;
            }


            @Override
            protected String doInBackground(String... params) {

            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Toast.makeText(context, "Please wait.", Toast.LENGTH_SHORT).show();
            }

            @Override
            protected void onPostExecute(String mymeaning) {

//since you have passed context to Toast in onPreExecute use that context here also.
                TextView myView  = new TextView(context);
                myView.setText(Html.fromHtml(myString));

                if(onTextViewCreatedListener!=null){
                    onTextViewCreatedListener.onTextViewCreated(myView);
                }
            }

        }

and then use AsyncTranslator class in your activity class like this:

AsyncTranslator asyncTranslator = new AsyncTranslator(new onTextViewCreatedListener() {
            @Override
            public void onTextViewCreated(TextView tv) {
                //you can use your created textview here
            }
        });

that context should be passed from your activity where you are going to call aynctask.execute()

Tharindu Welagedara
  • 2,660
  • 18
  • 27