在主要活动上填充另一个类的TextView

时间:2016-07-18 09:17:36

标签: java android android-asynctask

我在TextView表单MainActivity更新Async类时遇到问题Context我看到我需要将第二个类的MainActivity设置为那个 class RestOperation extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { //Java.net Http transaction happens here } @Override protected void onPostExecute(Void result) { //I wish to set the value of the TextView with the result here! } ,但我不知道如何在这种情况下实现这一点。我的应用程序看起来像这样。

XMLHttpRequest

6 个答案:

答案 0 :(得分:1)

你可以这样做:

#1 :为RestOperation课程制作一个构造函数,从Context获取Activity,如下所示:

    public RestOperation(MainActivity activityContext){
        this._activityContext = activityContext;
    }

#2 :在初始化Activity类时传递AsyncTask上下文,如下所示:

new RestOperation(MainActivity.this).execute("yours","params","here");

因此,您的AsyncTask类应该如下所示:

class RestOperation  extends AsyncTask<String, Void, String>  {
    MainActivity _activityContext;
    public RestOperation(MainActivity activityContext){
        this._activityContext = activityContext;
    }
    @Override
    protected String doInBackground(String... params) { 
        //Java.net Http    transaction happens here 
    }

    @Override
    protected void onPostExecute(String result) {
        ((TextView)_activityContext.findViewById(R.id.yourTextViewId)).setText(yourText);
    }
}

答案 1 :(得分:1)

  
      
  1. 如果您将创建将在onPostExecute上调用的接口,并且在Main活动中实现该接口并使用textview获取您的值和设置值,那么它将会很好。
  2.         

    2.newrestOperation(YourActivity.this,textviewobject).execute(&#34;你&#34;&#34; PARAMS&#34;&#34;此处&#34);

因此,您的AsyncTask类应如下所示:

Textview mtxt;
class RestOperation  extends AsyncTask<String, Void, Void>  {
    Context _activityContext;
    public RestOperation(Context activityContext,Textview txt){
        this._activityContext = activityContext;
         this.mtxt = txt;
    }
    @Override
    protected String doInBackground(String... params) { 
        //Java.net Http    transaction happens here 
    }

    @Override
    protected void onPostExecute(String result) {enter code here
        mtxt .setText(yourText);
    }
}

但#1是更好的方式。

答案 2 :(得分:0)

只需使用:

@Override
protected void onPostExecute(String result)
    {
        textView.setText(result);
    }

答案 3 :(得分:0)

将您的代码更改为:

class RestOperation  extends AsyncTask<String, Void, Void>  {

Context context; 
add public RestOperation (Contex cntx) {
this.context = cntx;
}
   @Override
    protected Void doInBackground(String... params) { //Java.net Http    transaction happens here }

@Override
   protected void onPostExecute(Void result) {
        //I wish to set the value of the TextView with the result here!
    }

当你完成这项任务时

new ResrOperation(getApplicationContext()).excute();

然后您可以访问主要活动中的项目

另一个选择

您可以在主要活动中覆盖onPostExecute方法,然后您也可以访问活动元素(在这种情况下没有上下文嵌入)

new ResrOperation(){
@Overide 
void onPostExecute(Long result) {
     // access elemets from activity
 }
}.excute();

答案 4 :(得分:0)

首先在interface

中写一个Async
CompleteListener completeListener;

public void setListener(CompleteListener cmpltListnr) {
   this.completeListener = cmpltListnr;
}

public interface CompleteListener {
    void OnCompleteListener(String result);
}   

onPostExecute

@Override
protected void onPostExecute(Void result) {
    if ( completeListener != null ) {
        completeListener.OnCompleteListener(""+result);
    }
}

implement您想要的Activity中的监听器。并覆盖该功能 并通过

this分配给界面
 yourAsynClassObject.setListener(this);

 @Override
 public void OnCompleteListener(String result) {
    textview.setText("" + result);
 }

答案 5 :(得分:0)

如果这是在另一个类中,你应该通过构造函数引用MainActivity(注意我在这里使用WeakReference,这样当活动被销毁时onPostExecute赢了执行(因为它不需要)并且AsyncTask不会泄漏内存)

//first you have to change the param (the last one) to String so your doInBackground would return String
class RestOperation  extends AsyncTask<String, Void, String>  {
private WeakReference<MainActivity> weakRef;

public RestOperation(MainActivity activity) {
    weakRef = new WeakReference<MainActivity>(activity);
}

@Override
protected String doInBackground(String... params) { 
//Java.net Http transaction happens here 
//You need to return some value here to set to the text view later
return "Something";
}

@Override
protected void onPostExecute(String result) {
    MainActivity activity = weakRef.get();
    if (activity != null) {
        //activity is still alive, updates the text view
        activity.updateText(result);
    }
}

在您的MainActivity中:

new RestOperation(this).execute("Your input param");

还在MainActivity中创建另一种方法:

public void updateText(String result) {
    textView.setText(result);
}