数据不会显示在async的onProgressUpdate中

时间:2017-09-30 13:07:27

标签: java android android-asynctask

目标:
显示文字&#34;测试&#34;基于使用异步&#39; v1 <- unlist(lst) i1 <- cumsum(c(!nzchar(v1)[-1], FALSE)) lst2 <- tapply(v1, i1, FUN = function(x) x[-(1:2)]) names(lst2) <- tapply(v1, i1, FUN = head, 1) lst2 #$A #[1] "aa" "bb" "cc" #$B #[1] "aa" "bb" "cc" "dd"

问题:
我不明白为什么代码没有显示文本&#34;测试&#34;。

我错过了哪一部分?

谢谢!

的信息:
*我是android的新手 *我使用的是API 24

MainActivity

onProgressUpdate

XML

package com.jfdimarzio.test1;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{

    TextView finalResult;
    TextView txt;
    Button mybutton;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        finalResult = (TextView) findViewById(R.id.txtview_display);
        mybutton = (Button) findViewById(R.id.btn_send);

        mybutton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                AsyncTaskRunner runner = new AsyncTaskRunner();
                runner.execute("test");
            }
        });

    }





        private class AsyncTaskRunner extends AsyncTask<String, String, String>
        {



            @Override
            protected String doInBackground(String... params)
            {
                return params[0];
            }


            @Override
            protected void onPostExecute(String text)
            {
                finalResult.setText("This is not from on ProgressUpdate");
            }

            @Override
            protected void onProgressUpdate(String... text)
            {
                finalResult.setText(text[0]);
            }
        }


    }

2 个答案:

答案 0 :(得分:1)

publishProgress(...)致电doInBackground()

示例:

        @Override
        protected String doInBackground(String... params)
        {
            publishProgress("test");
            return params[0];
        }

答案 1 :(得分:0)

因为您应该publishProgress()发布中间结果。在巡视中,当前代码return params[0];将在onPostExecute回调中收到。

所以将publishProgress(params[0])放入doInBackground回调中。

UPD

@Override
protected String doInBackground(String... params){
publishProgress(params[0]);// this will go into onProgressUpdate
return params[0];//return what you need in onPostExecute
}