[英]How do i modify an ArrayList from one Activity in a separate Activity
How do i add more Cars
to my ArrayList<Car>
from a separate Activity
? 如何从单独的
Activity
向我的ArrayList<Car>
添加更多Cars
?
Because I have something like this : 因为我有这样的事情:
private List<Car> getData() {
List<Car> cars = new ArrayList<Car>();
cars.add(new Car("Dodge", "Viper"));
cars.add(new Car("Chevrolet", "Corvette"));
cars.add(new Car("Aston Martin", "Vanquish"));
cars.add(new Car("Lamborghini", "Diablo"));
cars.add(new Car("Ford", "Pinto"));
return cars;
}
But cars are only example, I have textswitcher in one application and I want to get text from textswitcher and place it as new item in other Activity with listview. 但是汽车只是一个例子,我在一个应用程序中有textswitcher,我想从textswitcher中获取文本,并将其作为新项目放在带有listview的其他Activity中。
Just make your List a class variable and have an addCar function 只需将List设为一个类变量并具有addCar函数
public void addCar(String make,String model){
cars.add(new Car(make,model));
}
and change getData() 并更改getData()
private List<Car> getData() {
addCar("Dodge", "Viper");
addCar("Chevrolet", "Corvette");
etc...
return cars;
}
and in your other Activity just call addCar(make,model) 在您的其他Activity中,只需调用addCar(make,model)
Put cars
into intent, which starts second Activity
with putExtra()
. 将
cars
放到意图中,这将通过putExtra()
开始第二个Activity
。 Then retrieve it with getExtra()
and add the cars you want. 然后使用
getExtra()
检索它并添加所需的汽车。
There is no guarantee that the other Activity
will exist in memory once you leave it (as the system may destroy it if memory resources become low). 无法保证其他
Activity
一旦离开就将存在于内存中(因为如果内存资源变少,系统可能会销毁它)。 You should save your data to persistent storage (ie a database or SharedPreferences
) or pass the data as an Intent
extra instead. 您应该将数据保存到持久性存储(即数据库或
SharedPreferences
),或者将数据作为Intent
附加项传递。
Basically you are going to need to pass the object to the other Activity
for modification using Intents
. 基本上,您将需要使用
Intents
将对象传递给另一个Activity
进行修改。 HOWEVER , this seems very wrong . 但是 ,这似乎非常错误 。 I would suggest re-thinking how you are adding
Cars
to your ArrayList
outside of the Activity
. 我建议您重新考虑如何将
Cars
添加到Activity
之外的ArrayList
中。
As Harmeet Singh mentioned, you might want to look into creating a sqlite
table that will hold all your Cars
. 正如Harmeet Singh所述,您可能需要研究创建一个可容纳所有
Cars
的sqlite
表。 Then you can have your ListView
have a CursorAdapter
that is looking at that table. 然后,您可以让
ListView
具有正在查看该表的CursorAdapter
。 This will allow you add Cars
from anywhere and will be MUCH more flexible and better practice. 这将使您可以从任何地方添加
Cars
,并且将更加灵活和更好地实践。
Here are two posts that discuss passing the objects as Intents
to your other Activity
BUT again, i do not suggest you continue down this road to implement what you are asking. 这是两篇讨论如何将对象作为
Intents
再次传递到另一个Activity
BUT的文章,我不建议您继续沿着这条路实现您所要的内容。 Go with sqlite
and a database table. 使用
sqlite
和数据库表。
I would suggest you should use database like sqllite
or to share a common resource like a xml
file or any for permanent storage , this is not the way to make storage, as this type of storage is volatile . 我建议您应该使用
sqllite
数据库,或者共享一个xml
文件之类的公共资源或用于永久存储的任何资源 ,这不是进行存储的方法,因为这种类型的存储是易变的 。
All your data will go clean if memory clears in any case. 如果在任何情况下清除内存,所有数据将变得干净 。
have a look android persistent storage 看看android持久存储
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.