15

i have a method that does an SQLite database update and put this inside of an AsyncTask to make it faster and more reliable.

however there are two pieces of data that are needed to update the database. one is an Integer and the other is an object of the PrimaryKeySmallTank class that is shown here.

using the params array in the arguments of the doInBackground method of AsyncTask, i can pass an Integer in, but what if I have two different types of data like here?

if an integer is stored in int... params[0] i cannot store a different type object in params[1], so what can be done about this?

object i want to pass into the AsyncTask

public class PrimaryKeySmallTank {

int contractNumber;
int customerCode;
int septicCode;
String workDate;
int workNumber;

}

the AsyncTask that i am using

 public class UpdateInfoAsyncTask extends AsyncTask<Integer, Void, Void>{

  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub

  }

  @Override
  protected Void doInBackground(Integer... params) {
      Integer mIntegerIn = params[0];  // this is what I want to do, example
      PrimaryKeySmallTank mPrimaryKeySmallTank = params[1];  // different data type to pass in

      Database db = new Database(InspectionInfoSelectionList.this);
        db.openToWrite();
        db.updateWorkClassificationByRow(mPrimaryKeySmallTank, mIntegerIn);
        db.close();

           return null;
  }

 } // end UpdateInfoAsyncTask
Kevik
  • 9,181
  • 19
  • 92
  • 148
  • you can pass the required object in UpdateInfoAsyncTask () constructor. Just create a constructor for your async task – Rachita Nanda Aug 01 '13 at 09:19
  • does code placed inside of doInBackground method have access to the data that is a member variable of the UpdateAsyncTask class? like i can make member variables of the class and pass in data into the constructor for that – Kevik Aug 01 '13 at 09:22
  • @Kevik, Yes. Of course you can. As you can see in my answer, I have written that `use intValue` in `doInBackground` method. You can use those member variables as and when you need within that class scope. :) – Chintan Rathod Aug 01 '13 at 09:27
  • yes .Code placed inside of doInBackground method have access to the data that passed to the constructor .I have added an example also my answer – Rachita Nanda Aug 01 '13 at 09:29

3 Answers3

44

You should create a constructor for that.

public class UpdateInfoAsyncTask extends AsyncTask<Void, Void, Void>{
  int intValue;
  String strValue;

  public UpdateInfoAsyncTask(int intValue,String strValue){
      this.intValue = intValue;
      this.strValue = strValue;
  }

  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub

  }

  @Override
  protected Void doInBackground(Void... params) {
      //use intValue
      //use strValue
       return null;
  }
}

To use it

new UpdateInfoAsyncTask(10,"hi").execute();
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
6

Just pass the required object in the constructor of your async task and later use this object in doinBackground().You can create constructor and pass the your object in the following way:

public class MyAsyncTask extends AsyncTask<Void, Void, Void>{

    PrimaryKeySmallTank tankObj;

    public UpdateInfoAsyncTask(PrimaryKeySmallTank obj ){
        tankObj=obj;
    }

    @Override
    protected void onPreExecute() {
       // TODO Auto-generated method stub
    }

    @Override
    protected Void doInBackground(Void... params) {
        //code and use tankObj
         return null;
    }
}

In your code pass the required object:

new myAsyncTask(new PrimaryKeySmallTank (1,1,1,"hi",1).execute();
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
4

you can also try something like this :

private class xyz extends AsyncTask<Object, Void, String> {

@Override
protected String doInBackground(Object... objects) {

Long abc = (Long)objects[0];
String pqr = (String)objects[1];
.
.
.

Just a simple remark. In this way, it should be pointed out that you can not pass the primitive data (because the primitives can not be stored as an Object). For a primitive data the only way is to have the wrappers (like to Integer, Boolean etc.) converted then as usual. For example,

int i = (Integer) objects[0];
Dexter
  • 1,710
  • 2
  • 17
  • 34