2016-12-28 65 views
0

我有一个AsyncTask我需要将多个参数传递给我的构造函数。我知道它的错误做法是要传递太多的参数,并且更好地将方法分解成更小的块来避免这种情况,但是我不允许打破这个方法,所以我唯一的方法是找到一个更好的方法来传递参数。在我的AsyncTask中,我创建了一个构造函数。如何避免在AsyncTask中传递太多输入参数?

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

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; 
     } 
    } 
+0

您可以改为创建一个POJO,并把它传递给Aysnctask –

+0

您可以创建一个模型类,然后传递模型分成UpdateAsyncTask构造。同时在你的模型中创建setter和getters –

+0

http://www.javaworld.com/article/2074938/core-java/too-many-parameters-in-java-methods-part-3-builder-pattern.html – ajb

回答

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返回值做同样也是如此。

感谢

+0

我需要一个构造函数吗? (类似于我的异步任务?)@Sri – Baabidi

+0

嗨巴比迪,是的。我为构造函数,getter,setter,equals等用途的用户lombok库,目的是(https://projectlombok.org/)。将以下内容添加到您的依赖关系中。 – Sri

+0

@AxelH,当你有项目龙目岛时,你不必编码。 (它用一个词注释来减少代码的冗长部分)。你可以在这里阅读更多 - https:// zeroturnaround。com/rebellabs /为什么这个懒惰编码器在你会喜欢项目龙目岛/ – Sri

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); 
+0

你应该为你的班级使用更为标准的符号,'Model_class'应该是'ModelClass' – AxelH

+0

是的..我同意..我会更新它 – rafsanahmad007

+0

当您获取StockUtils..class的值时使用这些值初始化对象然后启动asynctask ... – rafsanahmad007