如何将两种不同的数据类型传递给

如何将两种不同的数据类型传递给

本文介绍了如何将两种不同的数据类型传递给 AsyncTask,Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以更新 SQLite 数据库并将其放入 AsyncTask 中,以使其更快、更可靠.

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

然而,更新数据库需要两条数据.一个是整数,另一个是此处显示的 PrimaryKeySmallTank 类的对象.

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.

在 AsyncTask 的 doInBackground 方法的参数中使用 params 数组,我可以传入一个整数,但是如果我有两种不同类型的数据,比如这里怎么办?

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?

如果一个整数被存储在 int...params[0] 中,我不能在 params[1] 中存储一个不同类型的对象,那么我能做些什么呢?

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?

我想传递给 AsyncTask 的对象

object i want to pass into the AsyncTask

public class PrimaryKeySmallTank {

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

}

我正在使用的 AsyncTask

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

推荐答案

你应该为此创建一个构造函数.

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

使用它

new UpdateInfoAsyncTask(10,"hi").execute();

这篇关于如何将两种不同的数据类型传递给 AsyncTask,Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:27