如何从另一个类访问一个ArrayList? [英] How can I access to an ArrayList from another class?
问题描述
我有一个的ArrayList
车
的:
的ArrayList<车>汽车=新的ArrayList<车>();
这是我补上我的的onCreate
方法我 MainActivity.java
与AsyncTask的:
新fillCars()执行();
我想这是使用此ArrayList 汽车
其他类,名为 ModifyCars.java
。我的问题是,经过了的AsyncTask
它的方法执行的onCreate
,在所有的类 MainActivity.java
该ArrayList 汽车
它填补,我可以用它里面的数据,但是当我将它传递给 ModifyCars的.java
它的大小等于 0
。
这是我使用的方法,通过这个的ArrayList
这是创建该类的一个空的构造 MainActivity.java
并创建一个get方法。在类 ModifyCars.java
创建 MainActivity
的对象,并尝试使用我之前创建get方法。在这里,code,我用于此目的的:
在 MainActivity.java
公共MainActivity(){
}
//下面的方法的onCreate
公众的ArrayList<车> getCars(){
返回车;
}
在 ModifyCars.java
MainActivity MA =新MainActivity();
Log.d(证明,Integer.toString(MA.getCars()的大小())); //这里的大小是0
我如何使用我的汽车
的ArrayList(当然,填充)在我的 ModifyCars.java
类?我究竟做错了什么?
我在网上搜索,但没有上过对我来说是有益的。任何帮助真的AP preciated。
在此先感谢!
您正在创建的MainActivity这就是为什么你得到一个空数组列表出现的新实例。 一个简单的解决方案,这将是使你arryalist静态的,访问它,如下,
在MainActivity:
静态的ArrayList<车>汽车=新的ArrayList<车>();
的ArrayList<车> myCars = MainActivity.cars
但是,一个标准的方式做这将是,发送数组列表中ModifyCars活动,同时呼吁其意图通过如下,
意向书我=新的意图(这一点,ModifyCars.class);
i.putExtra(CAR_ARRAY_LIST,this.cars);
startActivity(我);
和在ModifyCars活动,你应该从意图提取出来作为,
意图I = getIntent();
ArrayList的<车> myCars =(ArrayList的<车>)i.getSerializableExtra(CAR_ARRAY_LIST);
I have an ArrayList
of Car
:
ArrayList<Car> cars = new ArrayList<Car>();
that I fill on my onCreate
method of my MainActivity.java
with an AsyncTask:
new fillCars().execute();
What I want it's to use this ArrayList cars
in other class, named ModifyCars.java
. My problem it's that after the AsyncTask
it's executed on the method onCreate
, in all the class MainActivity.java
the ArrayList cars
it's fill and I can use the data inside of it but when I pass it to ModifyCars.java
the size it's equals to 0
.
The method that I use to pass this ArrayList
it's to create an empty constructor of the class MainActivity.java
and create a get method. In the class ModifyCars.java
I create an object of MainActivity
and try to use the get method that I created before. Here the code that I used for this purpose:
In MainActivity.java
public MainActivity(){
}
//Here the method onCreate
public ArrayList<Car> getCars(){
return cars;
}
In ModifyCars.java
MainActivity MA = new MainActivity();
Log.d("prove",Integer.toString(MA.getCars().size())); //Here the size it's 0
How can I use my cars
ArrayList (of course, filled) in my ModifyCars.java
class? What am I doing wrong?
I search on the Internet but nothing had been useful for me. Any help would really appreciated.
Thanks in advance!
You are creating a new instance of the MainActivity that's why you are getting an empty array list there. A simple solution to that would be making your arryalist static and access it as below,
In MainActivity :
static ArrayList<Car> cars = new ArrayList<Car>();
and in your ModifyCars.java access it as,
ArrayList<Car> myCars = MainActivity.cars
But a standard way to do this would be, sending the array list to the ModifyCars activity while calling it through intent as below,
Intent i = new Intent(this, ModifyCars.class);
i.putExtra("CAR_ARRAY_LIST", this.cars);
startActivity(i);
and in ModifyCars activity you should extract it from intent as,
Intent i = getIntent();
ArrayList<Car> myCars = (ArrayList<Car>) i.getSerializableExtra("CAR_ARRAY_LIST");
这篇关于如何从另一个类访问一个ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!