Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass in two different data types to AsyncTask, Android

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
like image 849
Kevik Avatar asked Aug 01 '13 09:08

Kevik


1 Answers

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();
like image 124
Chintan Rathod Avatar answered Oct 05 '22 23:10

Chintan Rathod