Skip to content Skip to sidebar Skip to footer

How To Generate Textview In Asynctask - Postexecute

I want to generate a TextView inside AsyncTask's onPostExecute like this : protected class AsyncTranslator extends AsyncTask { @Ove

Solution 1:

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

TextViewmyView=newTextView(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

TextViewmyView=newTextView(context);

Solution 2:

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.

publicinterfaceasyncTaskInterface {
    publicvoidprintEditText();
}

Then in your AsyncTask:

protectedclassAsyncTranslatorextendsAsyncTask<String, JSONObject, String>
{
   public asyncTaskInterface delegate;

    @OverrideprotectedStringdoInBackground(String... params) {

}

@OverrideprotectedvoidonPreExecute() {
    super.onPreExecute();

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

@OverrideprotectedvoidonPostExecute(String mymeaning) {

        delegate.printEditText();
}
}

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

publicclassmyClassActivityimplementsasyncTaskInterface ...

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.

Solution 3:

First create an interface like this:

publicinterfaceonTextViewCreatedListener {
        publicvoidonTextViewCreated(TextView tv);
    }

Then change your AsyncTranslator class like this

protectedclassAsyncTranslatorextendsAsyncTask<String, JSONObject, String>
        {

            private onTextViewCreatedListener onTextViewCreatedListener;

            publicAsyncTranslator(onTextViewCreatedListener onTextViewCreatedListener){
                this.onTextViewCreatedListener = onTextViewCreatedListener;
            }


            @OverrideprotectedStringdoInBackground(String... params) {

            }

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

            @OverrideprotectedvoidonPostExecute(String mymeaning) {

//since you have passed context to Toast in onPreExecute use that context here also.TextView myView  = newTextView(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 = newAsyncTranslator(newonTextViewCreatedListener() {
            @OverridepublicvoidonTextViewCreated(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()

Post a Comment for "How To Generate Textview In Asynctask - Postexecute"