AsyncTask的传递自定义对象 [英] AsyncTask passing custom objects

查看:253
本文介绍了AsyncTask的传递自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[我有一个自定义parcelable对象车辆]

[I have a custom parcelable object Vehicle]

我已经看过的AsyncTask 但它不是对这个话题太明确的:

I have looked at AsyncTask but it wasn't too clear on this topic:

我想一个字符串(这是车辆的车辆ID)传递到AsyncTask的,那么在doInBackground()我有一个方法

I would like to pass a String(which is the Vehicle ID of the Vehicle) into an AsyncTask, then in doInBackground() I have a method

mRemoteInterface.getTrackHistory();

这给了我一个ArrayList。我想那么,在onPostExecute()开始的活动都在这里的车辆ID和ArrayList是临时演员。

which gives me an ArrayList. I would like to then, in onPostExecute() start an activity where both the Vehicle ID and ArrayList are extras.

这是什么,我wan't到能够做一个大纲。问题是,我不明白,传递物体插入AsyncTask的,然后从doInBackground()来onPostExecute()和onPostExecute()回到原来的执行调用。

This is an outline of what I wan't to be able to do. The issue is that I don't understand passing objects INTO the asynctask, and then from doInBackground() to onPostExecute() and from onPostExecute() back to the original execute call.

getTrackHistory.execute(又该何去何从这里?);

getTrackHistory.execute(WHAT SHOULD GO HERE?);

private class getTrackHistory extends
AsyncTask<String, Integer, Boolean **WHAT SHOULD GO HERE?**> {

    @Override
    protected Boolean doInBackground(
            String... params) {
        try {
            String vehicleID = params[0];
            listVehicleHistory = (ArrayList<Vehicle>) mRemoteInterface.getVehicleHistory(vehicleID);
        } catch (Exception e) {

            e.printStackTrace();
        }


    }




    @Override
    protected void onProgressUpdate(Integer... progress) {

    }

    @Override
    protected void onPostExecute(Boolean worked) {

        super.onPostExecute(worked);

        Intent intent = new Intent(MainActivity.this,VehicleInfo.class);

        intent.putExtra("Vehicle ID", toReturn);
        intent.putParcelableArrayListExtra("VehicleHistory", listVehicleHistory);

        startActivity(intent);
    }

}

推荐答案

您可以将字符串传递给的AsyncTask的构造函数或doInbackground

You can pass the string to the constructor of asynctask or to doInbackground

   new getTrackHistory(mystringvalue).execute();

然后在构造

  private class getTrackHistory extends

{AsyncTask的

AsyncTask {

  String value; 
  public getTrackHistory(String mystring)
  {
      value= mystring;
  }

编辑:

您也可以通过值 doInbackground()

   new TheTask().execute("mystring");
   class TheTask extends AsyncTask <String,Void,Void>
    { 
     @Override
     protected void onPreExecute() {
    super.onPreExecute();
    }
    @Override
    protected void onPostExecute(Void result) 
     super.onPostExecute(result);

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

要在注释的问题

  new getTrackHistory(mystringvalue,ActivityName.this).execute();

在构造

  String value; 
  TheInterface listener;
  public getTrackHistory(String mystring,Context context)
  {
      value= mystring;
      listener = (TheInterface) context; 
  }

接口

public interface TheInterface {

public void theMethod(ArrayList<String> result); // your result type

 }

然后

在您doInbackground返回结果。我假设其类型为String的ArrayList。更改数组列表,以什么适合您的要求。

In your doInbackground return the result. I am assuming its ArrayList of type String. Change the arraylist to what suits your requirement.

在您的onPostExecute

In your onPostExecute

if (listener != null) 
{
  listener.theMethod(result); // result is the ArrayList<String>
  // result returned in doInbackground 
  // result of doInbackground computation is a parameter to onPostExecute 
}

在您的活动类实现接口

  public class ActivityName implements getTrackHistory.TheInterface

然后

 @Override
 public void theMethod(ArrayList<String> result) { // change the type of result according yo your requirement
 // use the arraylist here
 }

使用界面类似的帖子

Similar post using interface

<一个href=\"http://stackoverflow.com/questions/18129191/android-parse-json-stuck-on-get-task/18130111#18130111\">Android解析JSON停留在获取任务。

这篇关于AsyncTask的传递自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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