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

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

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.

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.


Solution 3:

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


Post a Comment for "How To Generate TextView In AsyncTask - PostExecute"