如何从我的 AsyncTask 返回一个 ArrayList? [英] How can I return an ArrayList from my AsyncTask?
问题描述
我的 MainActivity 上有一个 AsynkTask 方法,如下所示:
class chargeCars extends AsyncTask>{受保护的无效 onPreExecute(){}受保护的 ArrayListdoInBackground(Void... params) {ArrayList汽车 = 新的 ArrayList();汽车.添加(新汽车(1,汽车1");汽车.添加(新汽车(2,汽车2");汽车.添加(新汽车(3,汽车3");还车;}protected void onProgressUpdate(){}protected void onPostExecute(ArrayList c){}}
其中 Car
它是我在另一个 Class
中声明的对象以及它是哪个构造函数:
public Car(int idCar, String name){this.idCar = idCar;this.name = 名称;}
而且我知道 return cars;
将名为 cars
的 ArrayList
返回给方法 onPostExecute
但我试过了从那里检索这个 ArrayList
但我不能,因为它总是告诉我 onPostExecute
方法必须是 Void
.
我的 MainActivity
类中还声明了另一个 ArrayList
:
ArrayListtotalCars = new ArrayList();
所以我尝试将 cars
ArrayList 克隆到我的 onPostExecute
方法中的 totalCars
ArrayList 但它给了我一个错误说:
'clone()' 在 'java.lang.Object' 中有保护访问
这是试图克隆我的 ArrayList 的代码:
protected void onPostExecute(ArrayList c){对于(汽车cr:c){totalCars.add(cr.clone());}}
所以我也尝试将我的值直接添加到我的 doInBackground
方法中的 totalCars
ArrayList 中:
totalCars.add(new Car(1,"Car1");totalCars.add(new Car(2,"Car2");totalCars.add(new Car(3,"Car3");
但是当我执行我的 AsyncTask
方法时:
new chargeCars().execute();
我的 totalCars
ArrayList 的大小等于 0.
我该怎么办?我完全被阻止了,我在 SO 中搜索了答案,但找不到任何对我有帮助的东西.
我的Car
类是:
公共类汽车{私人int idCar;私人字符串名称;公共汽车(){};公共汽车(int idCar,字符串名称){this.idCar = idCar;this.name = 名称;}公共无效 setIdCar(int idCar){this.idCar = idCar;}公共 int getIdCar(){返回idCar;}公共无效集名称(字符串名称){this.name = 名称;}公共字符串 getName(){返回名称;}}
EDIT 2:我放了一些日志来看看我的程序到底做了什么,但我得到了奇怪的结果:
正如 Clairvoyant
所说,我使用了他的两种方法,但其中任何一种都有效.我能看到什么?好吧,我看到在 onPostExecute
方法中,ArrayList 包含两个方法中的值.这里没有问题.
问题是当我在我的 MainActivity
类的方法 onCreate
中执行 AsyncTask 时,我的 ArrayList totalCars
没有任何value 里面,但在我的 AsyncTask 的方法 onPostExecute
中,它具有我放在 ArrayList 中的所有值.
可能是什么问题?
编辑 3:我也试过:
公共类 MainActivity 扩展 ActionBarActivity {ArrayList汽车 = 新的 ArrayList();protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);新的chargeCars().execute();Log.d("Size onCreate: ", "" + cars.size());//它返回给我一个 0 --> 不正确}类chargeCars 扩展了AsyncTask{受保护的无效 onPreExecute(){}受保护的 ArrayListdoInBackground(Void... params) {汽车.添加(新汽车(1,汽车1");汽车.添加(新汽车(2,汽车2");汽车.添加(新汽车(3,汽车3");Log.d("Size: ", "" + cars.size());//它给我的大小等于 3 --> 正确返回真;}protected void onProgressUpdate(){}protected void onPostExecute(boolean c){}}}
但是还是不行.我还尝试制作一个 ProgressDialog
以确保我的 AsyncTask 在我尝试在我的 onCreate
方法中使用我的 Log
时它已完成但它也给了我一个错误(我在 SO 中有另一个关于它的问题,但我不知道在 SO 中直接引用另一个问题是否是一个好习惯,所以我不在这里发布).
我做错了吗?为什么在我的 doInBackground
中它给了我 ArrayList
的正确 size
而在我的 onCreate
方法中没有?>
提前致谢!
我做错了吗?为什么在我的 doInBackground 中它给了我ArrayList 的大小是否正确,而在我的 onCreate 方法中不正确?
因为执行是异步的,并且 UI 线程
不会结束 AsyncTask
的执行以继续其执行流程.要返回结果,当您的 AsyncTask
完成执行时,不阻塞 UI 线程
,您可以使用委托/列表器.
让我们在您的 Activity 顶部声明一个接口,并让它实现这个接口(您需要实现这些方法).当 onCarsCompleted
被调用时,您将能够打印您的 ArrayList
公共类 MainActivity 扩展 ActionBarActivity 实现 OnCarsListener {公共接口 OnCarsListener {void onCarsCompleted(ArrayListcars);void onCarsError(字符串错误);}//其他代码public void onCarsCompleted(ArrayListcars) {Log.d("Size: ", "" + cars.size());}公共无效 onCarsError(字符串错误){}}
现在我们为您的 AsyncTask
添加一个构造函数,该构造函数将 OnCarsListener
类型的对象作为参数,并且我们在 OnCarsListener
的范围内保留对该对象的成员引用code>chargeCars 和 onPostExecute
我们将计算结果返回给 Activity
:
class chargeCars extends AsyncTask>{私人最终 OnCarsListener mListener;ArrayList汽车 = 新的 ArrayList();public chargeCars(OnCarsListener 监听器) {mListener = 监听器;}受保护的无效 onPreExecute(){}受保护的 ArrayListdoInBackground(Void... params) {汽车.添加(新汽车(1,汽车1");汽车.添加(新汽车(2,汽车2");汽车.添加(新汽车(3,汽车3");Log.d("Size: ", "" + cars.size());//它给我的大小等于 3 --> 正确还车;}protected void onProgressUpdate(){}protected void onPostExecute(ArrayListcars){如果(mListener != null){mListener.onCarsCompleted(汽车);}}}
当然,你需要像这样实例化AsyncTask
new chargeCars(this).execute();
I have an AsynkTask method on my MainActivity that it's the following:
class chargeCars extends AsyncTask<Void, Integer, ArrayList<Car>> {
protected void onPreExecute(){
}
protected ArrayList<Car> doInBackground(Void... params) {
ArrayList<Car> cars = new ArrayList<Car>();
cars.add(new Car(1,"Car1");
cars.add(new Car(2,"Car2");
cars.add(new Car(3,"Car3");
return cars;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(ArrayList<Car> c){
}
}
where Car
it's an object that I have declared in another Class
and which constructor it's:
public Car(int idCar, String name)
{
this.idCar = idCar;
this.name = name;
}
and I know that return cars;
returns the ArrayList
named cars
to the method onPostExecute
but I tried to retrieve this ArrayList
from there and I couldn't because it always said to me that onPostExecute
method have to be Void
.
I have also another ArrayList
declared in my MainActivity
class:
ArrayList<Car> totalCars = new ArrayList<Car>();
so what I tried it's to clone cars
ArrayList into my totalCars
ArrayList in my onPostExecute
method but it gives me an error in which it says that:
'clone()' has protected access in 'java.lang.Object'
Here it's the code trying to clone my ArrayList:
protected void onPostExecute(ArrayList<Car> c)
{
for(Car cr: c)
{
totalCars.add(cr.clone());
}
}
So I also tried to add my values directly to my totalCars
ArrayList on my doInBackground
method:
totalCars.add(new Car(1,"Car1");
totalCars.add(new Car(2,"Car2");
totalCars.add(new Car(3,"Car3");
but when I execute my AsyncTask
method:
new chargeCars().execute();
the size of my totalCars
ArrayList it's equals to 0.
What should I do? I'm totally block and I searched in SO for an answer but I couldn't find anything that helps to me.
EDIT: My Car
Class it's:
public class Car {
private int idCar;
private String name;
public Car(){};
public Car(int idCar, String name)
{
this.idCar = idCar;
this.name = name;
}
public void setIdCar(int idCar)
{
this.idCar = idCar;
}
public int getIdCar()
{
return idCar;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
EDIT 2: I put some logs to see what exactly does my program and I got strange results:
As Clairvoyant
said, I use both of his methods but any of them works. What could I saw? Well, I saw that in the onPostExecute
method the ArrayList contains values in both of the methods. Here there is no problem.
The problem it's that when I execute the AsyncTask in the method onCreate
of my MainActivity
class, my ArrayList totalCars
doesn't have any value inside, but in the method onPostExecute
of my AsyncTask it has all the values that I put inside the ArrayList.
What could be the problem?
EDIT 3: I also tried with:
public class MainActivity extends ActionBarActivity {
ArrayList<Car> cars = new ArrayList<Car>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new chargeCars().execute();
Log.d("Size onCreate: ", "" + cars.size()); //It returns me a 0 -->Incorrect
}
class chargeCars extends AsyncTask<Void, Integer, Boolean> {
protected void onPreExecute(){
}
protected ArrayList<Car> doInBackground(Void... params) {
cars.add(new Car(1,"Car1");
cars.add(new Car(2,"Car2");
cars.add(new Car(3,"Car3");
Log.d("Size: ", "" + cars.size()); //It gives me the size equals to 3 -->Correct
return true;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(boolean c){
}
}
}
But still doesn't work. I also tried to make a ProgressDialog
to be secure that my AsyncTask it's finished when I try to use my Log
in my onCreate
method but it also gives me an error (I have another question in SO about it but I don't know if it's a good practice in SO to make directly reference from another question so I don't post it here).
Am I doing something wrong? Why in my doInBackground
it gives me the correct size
of the ArrayList
and in my onCreate
method not?
Thanks in advance!
Am I doing something wrong? Why in my doInBackground it gives me the correct size of the ArrayList and in my onCreate method not?
Because the execution is asynchronous and the UI Thread
doesn't way the end of the execution of your AsyncTask
to continue its execution flow. To get the results back, when your AsyncTask
finishes its execution, without blocking the UI Thread
, you could use a delegate/lister.
Let's declare an interface on the top of your Activity, and let't it implement this interface (you'll need to implement the methods). When onCarsCompleted
is invoked you'll be able to print the correct size of your ArrayList
public class MainActivity extends ActionBarActivity implements OnCarsListener {
public interface OnCarsListener {
void onCarsCompleted(ArrayList<Car> cars);
void onCarsError(String error);
}
// the other code
public void onCarsCompleted(ArrayList<Car> cars) {
Log.d("Size: ", "" + cars.size());
}
public void onCarsError(String error) {
}
}
now we add a constructor to your AsyncTask
, which takes as parameter an object of type OnCarsListener
, and we keep a member reference to the object in the scope of chargeCars
, and onPostExecute
we communicate back to the Activity
the results of your computation:
class chargeCars extends AsyncTask<Void, Integer, ArrayList<Car>> {
private final OnCarsListener mListener;
ArrayList<Car> cars = new ArrayList<Car>();
public chargeCars(OnCarsListener listener) {
mListener = listener;
}
protected void onPreExecute(){
}
protected ArrayList<Car> doInBackground(Void... params) {
cars.add(new Car(1,"Car1");
cars.add(new Car(2,"Car2");
cars.add(new Car(3,"Car3");
Log.d("Size: ", "" + cars.size()); //It gives me the size equals to 3 -->Correct
return cars;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(ArrayList<Car> cars){
if (mListener != null) {
mListener.onCarsCompleted(cars);
}
}
}
And of course, you need to instantiate the AsyncTask like
new chargeCars(this).execute();
这篇关于如何从我的 AsyncTask 返回一个 ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!