jason
jason

Reputation: 7164

How to generate TextView in AsyncTask - PostExecute

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.

Upvotes: 0

Views: 472

Answers (3)

Tharindu Welagedara
Tharindu Welagedara

Reputation: 2725

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()

Upvotes: 0

Blady214
Blady214

Reputation: 739

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.

Upvotes: 1

ThomasThiebaud
ThomasThiebaud

Reputation: 11969

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);

Upvotes: 3

Related Questions