如何在AsyncTask中生成TextView-PostExecute [英] How to generate TextView in AsyncTask - PostExecute

查看:37
本文介绍了如何在AsyncTask中生成TextView-PostExecute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样在AsyncTask的onPostExecute内生成一个TextView:

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

但是它给出了错误,告诉我这不能应用于AsyncTranslator. 您能告诉我如何在AsyncTask onPostExecute内生成textViews吗?谢谢.

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.

推荐答案

来自文档可能的构造函数是

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

但是你在做

TextView myView  = new TextView(this);

AsyncTranslator内部,这是不正确的.

inside AsyncTranslator which is incorrect.

如果对上下文有引用,则可以在AsyncTask中轻松创建TextView.请参阅此线程以获取对上下文的引用.

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

这篇关于如何在AsyncTask中生成TextView-PostExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆