如何避免在AsyncTask中传递太多输入参数?

时间:2016-12-28 06:40:56

标签: java android object android-asynctask

我有AsyncTask我需要将多个参数传递给构造函数。我知道传递太多参数的不良做法,最好将方法分解为更小的块来避免这种情况,但是我不允许破坏方法所以我唯一的办法就是找到一种替代方法以更好的方式传递参数。在我的 class UpdateAsyncTask extends AsyncTask<Void, Void, Void> { private String productId; int mutationAmount; boolean isOpeningStock; FlightLegIdentifier fli; String crew; String tabletId; UpdateAsyncTask(final String productId, final int mutationAmount, final boolean isOpeningStock, final String flightKey, final FlightLegIdentifier fli, final String crew, final String tabletId, final AirFiApplication airFiApplication) { this.productId = productId; this.mutationAmount = mutationAmount; this.isOpeningStock = isOpeningStock; this.fli = fli; this.tabletId = tabletId; this.crew = crew; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); productStockAdapter.notifyDataSetChanged(); } @Override protected Void doInBackground(Void... params) { StockUtils.saveMutation(productId, mutationAmount, isOpeningStock, flightKey, fli, crew, tabletId, getAirFiApplication()); return null; } } 中,我创建了一个构造函数。

是否可以使用值对象创建模型类并传递它们?我需要吸气剂和固定剂吗?

[IntentFilter(new string[] { Intent.ActionView },
Categories = new  string[] { Intent.ActionDefault, Intent.CategoryBrowsable,
Intent.ActionSend, Intent.ActionSendMultiple },
DataScheme = "mimetype",
DataPathPattern = "*/*",
DataHost = "*.*")]

2 个答案:

答案 0 :(得分:3)

您可以创建模型类并将其作为参数传递给AsyncTask

这是一个例子:

 private static class ModelClass {
    int length;
    int height;

    ModelClass(int l, int h) {
        this.length = l;
        this.height = h;
    }
}

定义AsyncTask任务

 private class MyTask extends AsyncTask <ModelClass, Void, Void> {


    @Override
     protected void onPreExecute(){

        }

    @Override
    protected void doInBackground(ModelClass... params) {
        int length = params[0].length;
        int height = params[0].height;
        ...
        //here u can perform your saveMutation() function using the parameters...
    }

    @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
}

使用Aysnctask

 //Initialize model class
ModelClass params = new ModelClass(10,10);

//pass it to asynch tash
MyTask myTask = new MyTask();
myTask.execute(params);

答案 1 :(得分:0)

您可以创建一个定义要传递给AsyncTask的参数的类。

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@AllArgsConstructor(suppressConstructorProperties=true)
@Getter
@Setter
class Product {
  private String productId; 
  private int mutationAmount; 
  private boolean isOpeningStock;
  private String flightKey;
  private FlightLegIdentifier fli;
  private String crew;
  private String tabletId;
  private AirFiApplication airFiApplication;
} 

class UpdateAsyncTask extends AsyncTask<Product, Void, Void> {
  ...
  ...
  @Override
  protected Void doInBackground(Product... params) {
  }
}

我使用Project Lombok进行注释(https://projectlombok.org/)。 您必须将以下内容添加到build.gradle

dependencies { ... compile 'org.projectlombok:lombok:1.12.6' }

注意:您也可以对AsyncTask的返回值执行相同操作。

由于