Android 如何从AsyncTask修改MainActivity的TextView?

Android 如何从AsyncTask修改MainActivity的TextView?,android,android-asynctask,Android,Android Asynctask,我正在编写一个聊天应用程序,它有两个进程(Get和Send)。它们写在MainActivity中,工作方式如下: Send: //Called when button is pressed (1) Get String from editText into MSG (2) Run AsyncTask_SEND(MSG).executeOnExecutor(...) to send (this procedure avoids internet calls at main thread (

我正在编写一个聊天应用程序,它有两个进程(Get和Send)。它们写在MainActivity中,工作方式如下:

Send: //Called when button is pressed
(1) Get String from editText into MSG
(2) Run AsyncTask_SEND(MSG).executeOnExecutor(...) to send
    (this procedure avoids internet calls at main thread (NetworkOnMainThreadException))
(3) TextView.setText( TextView.getText() + "Local : " + MSG + "\n" );


Get: //Called at onCreate
Run AsyncTask_GET().executeOnExecutor(...) to get messages.
It's doInBackground works like this:
|    (1) Declare String MSG
|    (2) While client still has messages to send (kind of infinite loop):
|    |    (2.1) Get client's next line into MSG
|    |    (2.2) TextView.setText( TextView.getText() + "Remote : " + MSG + "\n" );
问题是Get的步骤(2.2)显然不起作用

相反,我使用Log.d(“Remote”,MSG)在LogCat控制台打印远程消息


我的问题是:鉴于此TextView位于MainActivity(并且AsyncTask\u GET是一个独立的类),如何从AsyncTask\u GET修改此TextView?

Async task GET buildin方法就是为了这个。它被称为onProgressUpdate,并且在ui线程上运行

private class DownloadFilesTask extends AsyncTask<URL, String, Long> {
     protected Long doInBackground(URL... urls) {

             publishProgress("hello from async task"); 
            // you call it just like this, but remember its array

         }
         return totalSize;
     }

     protected void onProgressUpdate(String... string) {

      textView.settext(string[0]); // and here you set the text

     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
publishProgress(“来自异步任务的hello”);
//你这样称呼它,但记住它的数组
}
返回总大小;
}
受保护的void onProgressUpdate(字符串…字符串){
textView.settext(字符串[0]);//在这里设置文本
}
受保护的void onPostExecute(长结果){
showDialog(“下载的”+结果+“字节”);
}
}

异步任务为此获得了构建方法。它被称为onProgressUpdate,并且在ui线程上运行

private class DownloadFilesTask extends AsyncTask<URL, String, Long> {
     protected Long doInBackground(URL... urls) {

             publishProgress("hello from async task"); 
            // you call it just like this, but remember its array

         }
         return totalSize;
     }

     protected void onProgressUpdate(String... string) {

      textView.settext(string[0]); // and here you set the text

     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
publishProgress(“来自异步任务的hello”);
//你这样称呼它,但记住它的数组
}
返回总大小;
}
受保护的void onProgressUpdate(字符串…字符串){
textView.settext(字符串[0]);//在这里设置文本
}
受保护的void onPostExecute(长结果){
showDialog(“下载的”+结果+“字节”);
}
}

异步任务为此获得了构建方法。它被称为onProgressUpdate,并且在ui线程上运行

private class DownloadFilesTask extends AsyncTask<URL, String, Long> {
     protected Long doInBackground(URL... urls) {

             publishProgress("hello from async task"); 
            // you call it just like this, but remember its array

         }
         return totalSize;
     }

     protected void onProgressUpdate(String... string) {

      textView.settext(string[0]); // and here you set the text

     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
publishProgress(“来自异步任务的hello”);
//你这样称呼它,但记住它的数组
}
返回总大小;
}
受保护的void onProgressUpdate(字符串…字符串){
textView.settext(字符串[0]);//在这里设置文本
}
受保护的void onPostExecute(长结果){
showDialog(“下载的”+结果+“字节”);
}
}

异步任务为此获得了构建方法。它被称为onProgressUpdate,并且在ui线程上运行

private class DownloadFilesTask extends AsyncTask<URL, String, Long> {
     protected Long doInBackground(URL... urls) {

             publishProgress("hello from async task"); 
            // you call it just like this, but remember its array

         }
         return totalSize;
     }

     protected void onProgressUpdate(String... string) {

      textView.settext(string[0]); // and here you set the text

     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
publishProgress(“来自异步任务的hello”);
//你这样称呼它,但记住它的数组
}
返回总大小;
}
受保护的void onProgressUpdate(字符串…字符串){
textView.settext(字符串[0]);//在这里设置文本
}
受保护的void onPostExecute(长结果){
showDialog(“下载的”+结果+“字节”);
}
}

如果它是独立类,则可以向AsyncTask类添加构造函数并向其发送TextView,然后可以在
onPostExecute()
方法中更改其文本,如:

public class GetMsg extends AsyncTask<Void, Void, Void> {
    private TextView msg;

    public GetMsg(TextView msg){
        this.msg = msg;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Change the text of msg TextView
        msg.setText("Add your text here");
}

如果它是独立类,则可以向AsyncTask类添加构造函数并向其发送TextView,然后可以在
onPostExecute()
方法中更改其文本,如:

public class GetMsg extends AsyncTask<Void, Void, Void> {
    private TextView msg;

    public GetMsg(TextView msg){
        this.msg = msg;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Change the text of msg TextView
        msg.setText("Add your text here");
}

如果它是独立类,则可以向AsyncTask类添加构造函数并向其发送TextView,然后可以在
onPostExecute()
方法中更改其文本,如:

public class GetMsg extends AsyncTask<Void, Void, Void> {
    private TextView msg;

    public GetMsg(TextView msg){
        this.msg = msg;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Change the text of msg TextView
        msg.setText("Add your text here");
}

如果它是独立类,则可以向AsyncTask类添加构造函数并向其发送TextView,然后可以在
onPostExecute()
方法中更改其文本,如:

public class GetMsg extends AsyncTask<Void, Void, Void> {
    private TextView msg;

    public GetMsg(TextView msg){
        this.msg = msg;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Change the text of msg TextView
        msg.setText("Add your text here");
}

您有两个选择,首先是post-execute方法。第二步,在构造函数中传递活动实例,然后在doinbackground方法中运行到runnable,如adam所说。我选择了第一个选项。您有两个选项,第一个是post-execute方法。第二步,在构造函数中传递活动实例,然后在doinbackground方法中运行到runnable,如adam所说。我选择了第一个选项。您有两个选项,第一个是post-execute方法。第二步,在构造函数中传递活动实例,然后在doinbackground方法中运行到runnable,如adam所说。我选择了第一个选项。您有两个选项,第一个是post-execute方法。第二步,在构造函数中传递活动实例,然后在doinbackground方法中运行到runnable,如adam所说。我选择了第一个,成功了!!感谢@Adam Fręśko:)我将mainactivity textview提供给asynctask的构造函数,并在onProgressUpdate上成功更新了它。它成功了!!感谢@Adam Fręśko:)我将mainactivity textview提供给asynctask的构造函数,并在onProgressUpdate上成功更新了它。它成功了!!感谢@Adam Fręśko:)我将mainactivity textview提供给asynctask的构造函数,并在onProgressUpdate上成功更新了它。它成功了!!谢谢@Adam Fręśko:)我将mainactivity textview提供给异步任务的构造函数,并在onProgressUpdate上成功更新了它。最佳答案,谢谢最佳答案,谢谢最佳答案,谢谢最佳答案,谢谢最佳答案,谢谢