Java 如何将对象传递给AsyncTask?
我有一个对象Java 如何将对象传递给AsyncTask?,java,android,android-asynctask,Java,Android,Android Asynctask,我有一个对象Car,它具有以下构造函数: public Car(int idCar, String name) { this.idCar = idCar; this.name = name; } 在这里我没有任何问题,所以我创建了一个名为newCar的对象Car,如下所示: Car newCar = new Car(1,"StrongCar"); class modifyCar extends AsyncTask<Car, Integer, ArrayList<E
Car
,它具有以下构造函数:
public Car(int idCar, String name)
{
this.idCar = idCar;
this.name = name;
}
在这里我没有任何问题,所以我创建了一个名为newCar
的对象Car
,如下所示:
Car newCar = new Car(1,"StrongCar");
class modifyCar extends AsyncTask<Car, Integer, ArrayList<Evento>> {
protected void onPreExecute()
{
}
protected ArrayList<Evento> doInBackground(Car... newCarAsync)
{
//The rest of the code using newCarAsync
}
protected void onProgressUpdate()
{
}
protected void onPostExecute()
{
}
}
我的问题是我想把这个新车
作为参数传递给我的异步任务
,名为modifyCar
,但我不知道怎么做
我在SO中搜索,发现了以下问题:
但它并没有解决我的问题,因为在给出的解决方案中,它们只将字符串
传递给异步任务
,而不是整个对象
我想要的是将整个对象作为参数传递给AsyncTask。
根据我在上面的问题中给出的解决方案,我尝试将对象传递给AsyncTask
new modifyCar(newCar).execute();
因此,我声明AsyncTask如下:
Car newCar = new Car(1,"StrongCar");
class modifyCar extends AsyncTask<Car, Integer, ArrayList<Evento>> {
protected void onPreExecute()
{
}
protected ArrayList<Evento> doInBackground(Car... newCarAsync)
{
//The rest of the code using newCarAsync
}
protected void onProgressUpdate()
{
}
protected void onPostExecute()
{
}
}
class modifyCar扩展异步任务{
受保护的void onPreExecute()
{
}
受保护的ArrayList doInBackground(汽车…newCarAsync)
{
//其余代码使用newCarAsync
}
受保护的void onProgressUpdate()
{
}
受保护的void onPostExecute()
{
}
}
但我不知道这是否正确。如果没有,我应该为此做些什么
提前谢谢 如果对象是
String
或Car
或AbstractDeathRayController
的实例,则将它们传递给异步任务的预期方式是通过以下方法:
顺便说一句,Java将使用CamelCase,因此最好将类重命名为ModifyCar
您应该在execute方法上传递Car对象。您可以在文档()中阅读它:
异步任务由3种泛型类型定义,称为Params,
进度和结果,以及4个步骤,称为onPreExecute,doInBackground,
onProgressUpdate和onPostExecute
遵循文档示例:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
你读到的解决方案是对的,你只是做错了。您需要轻松地为AsyncTask类创建构造函数并将对象传递给它
class modifyCar extends AsyncTask<Void, Integer, ArrayList<Evento>> {
private Car newCar;
// a constructor so that you can pass the object and use
modifyCar(Car newCar){
this.newCar = newCar;
}
protected void onPreExecute()
{
}
protected ArrayList<Evento> doInBackground(Void... parms)
{
//The rest of the code using newCarAsync
}
protected void onProgressUpdate()
{
}
protected void onPostExecute()
{
}
}
也许可以在doInBackground()
中演示如何访问它,因为有些人很难理解varargs。正如@DanielNugent所说,我需要知道如何在doInBackground()
方法中访问它,因为我不太明白您到底想说什么。但是,在doInBackground()
方法中,如何在其中使用对象汽车?或者newCarAsync
它与在AsyncTask
中声明的对象newCar
相同?@Error404您创建的对象现在被传递到类内部的类或doInBackground()
它是newCar
,doInBackground()
方法中的newCarAsync
变量的用途是什么?@Error404没用,我只是不想更改你的类,我在上面编辑了这个类,非常感谢!你帮了我很多
// pass the object that you created
new modifyCar(newCar).execute();