【问题标题】:can't return data from inner asynctask class to outer class无法将数据从内部 asynctask 类返回到外部类
【发布时间】:2014-09-04 05:32:52
【问题描述】:

嗨朋友们,我知道有很多关于这个话题的问题,但我无法从他们那里得到任何结果。我正在使用我的 ClassIsInternalParser 解析 xml 数据扩展默认处理程序。我在内部类 PostAsync extends AsyncTask 的活动中使用此类

但原因是我无法将我在 PostAsync 类中收集的数据返回给主要活动。它只设置 null

这是我的代码

package com.example.uiexercisesplash;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ClassIsInternalListViewActivity extends Activity implements  OnClickListener{  



TextView tv, tv2;
Button back;

String[][] array = new String[10][3];

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

    new PostAsync().execute();

 tv=(TextView) findViewById(R.id.tatar);
 tv2= (TextView) findViewById(R.id.tatar2);

  tv2.setText(array[0][0]);   //Why this sets null!!

 back= (Button) findViewById(R.id.back);
 back.setOnClickListener(this);

}   

class PostAsync extends AsyncTask<Void, Void,String[][]>{

    ProgressDialog pd;
    ClassIsInternalParser  parser;

    @Override
    protected void onPreExecute() {
        //we can set up variables here
        pd = ProgressDialog.show(ClassIsInternalListViewActivity.this,
"Classisinternal","Loading last post...",true,false);   
    }

    protected void onPostExecute(String[][] result) {

        //in this way it sets correctly 
        tv.setText(result[0][0]);  


         array=result;

    pd.dismiss();

        pd.cancel();
    }

    protected String[][] doInBackground(Void... params) {
        parser = new ClassIsInternalParser();
        parser.get();

        return parser.dataArray;
    }
    }


@Override
public void onClick(View v) {
            finish();   
    }

}

【问题讨论】:

  • doInbackground 返回参数到后执行

标签: android android-asynctask android-parser


【解决方案1】:

尝试创建 PostAsync 对象并从该对象调用execute()。之后调用get() 方法来检索您的返回数组,如下所示:

PostAsync obj=new PostAsync(this);
obj.execute();
String[][] array=obj.get();
tv2.setText(array[0][0]);

【讨论】:

  • .get() 基本上将异步过程变成了同步过程,因为它会阻塞并等待结果。
  • 我注意到它阻塞了,我会记住它再次感谢
【解决方案2】:

tv2.setText(array[0][0]); 为空,因为您在设置值之前获取数组值,即tv2.setText(array[0][0]); 在完成 Asynctask 之前执行。

所以在onPostExecute方法中做这一步

protected void onPostExecute(String[][] result) {

        //in this way it sets correctly 
        tv.setText(result[0][0]);  
        tv2.setText(result[0][2]);

         array=result;
         tv2.setText(array[0][0]);// add here
    pd.dismiss();

        pd.cancel();
    }

【讨论】:

  • 谢谢,但有什么方法可以将变量传递给数组。实际上我会将该数组用于 ListView 而不仅仅是 tv2
  • @user3778620 你可以在onPostexecute方法中设置listview。
  • @GiruBhai 如果我为 AsyncTask 使用单独的类,那么我如何访问主 ui 线程中的返回内容。
  • @ved 然后将其用作像这样的回调stackoverflow.com/questions/14253421/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多