如何通过AsyncTask活动为textview设置文本? [英] How can I set the text for a textview from an AsyncTask activity?

查看:154
本文介绍了如何通过AsyncTask活动为textview设置文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Fragment Activity,其中包含一个textview和另一个扩展AsyncTask的类.现在,我想使用onPostExecute(String result)方法在片段活动的textview中设置结果文本.

I have got a Fragment Activity that contains a textview and another class that extends AsyncTask. Now I would like to use the onPostExecute(String result) method to set the result text in my textview that is in my fragment activity.

我该怎么做?我已经为AsyncTask类创建了一个自定义构造函数,该构造函数接受一个上下文对象.我该怎么用??

How can I do that? I already created a custom constructor for the AsyncTask class that takes in a context object. How can I use that??

这是我在Fragment活动中创建任务对象的方式:

This is how I create a task object in my Fragment activity:

String query = "someText";
Task task = new Task(this.getActivity());
task.execute(query);

这是我的任务类的摘录:

This is a snippet from my task class:

public class Task extends AsyncTask<String, Void, String> {

    private Context context;

    public Task (Context context) {
        this.context = context;
    }

    protected void onPostExecute(String result) {
    super.onPostExecute(result);
    // ??? What comes here ???
    }
}

推荐答案

TextView txt = (TextView)((Activity)context).findViewById(R.id.watheveryouwant);
txt.setText("blabla");

但是您应该传递一个Activity而不是一个Context,这样会更容易;-)

But you should pass an Activity and not a Context, will be easier ;-)

    public Task (Context context, TextView t) {
        this.context = context;
        this.t = t;
    }
   super.onPostExecute(result);
        t.setText("BlahBlah")
    }

应该做到这一点

这篇关于如何通过AsyncTask活动为textview设置文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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