0

How do i add more Cars to my ArrayList<Car> from a separate Activity?

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.

5 Answers5

0

Just make your List a class variable and have an addCar function

public void addCar(String make,String model){
      cars.add(new Car(make,model));
}

and change getData()

    private List<Car> getData() {

            addCar("Dodge", "Viper");
            addCar("Chevrolet", "Corvette");
            etc...

        return cars;

    }

and in your other Activity just call addCar(make,model)

Mario
  • 2,681
  • 1
  • 20
  • 23
  • 1
    In the second `Activity` he would need a reference to the first one to do it, wouldn't he? – alex Aug 02 '12 at 17:23
  • yes, shouldn't be a problem though. Although I don't know what his class/code looks like. He doesn't state any constraints – Mario Aug 02 '12 at 17:24
  • 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. – user1560197 Aug 02 '12 at 17:37
  • You really should look into `sqlite` and creating a table for your data. Take a look at my answer for more detail. Attempting to do what you are saying is not good practice and would be a major headache to change and implement. http://stackoverflow.com/a/11782674/427763 – prolink007 Aug 02 '12 at 17:47
0

Put cars into intent, which starts second Activity with putExtra(). Then retrieve it with getExtra() and add the cars you want.

alex
  • 9,184
  • 12
  • 64
  • 86
0

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). You should save your data to persistent storage (i.e. a database or SharedPreferences) or pass the data as an Intent extra instead.

Alex Lockwood
  • 81,274
  • 37
  • 197
  • 245
0

Basically you are going to need to pass the object to the other Activity for modification using Intents. HOWEVER, this seems very wrong. I would suggest re-thinking how you are adding Cars to your ArrayList outside of the Activity.

As Harmeet Singh mentioned, you might want to look into creating a sqlite table that will hold all your Cars. Then you can have your ListView have a CursorAdapter that is looking at that table. This will allow you add Cars from anywhere and will be MUCH more flexible and better practice.

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. Go with sqlite and a database table.

  1. How to pass object from one activity to another in Android
  2. How do I pass data between activities in Android?
Community
  • 1
  • 1
prolink007
  • 30,784
  • 21
  • 111
  • 173
0

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.

All your data will go clean if memory clears in any case.

have a look android persistent storage

Harmeet Singh
  • 2,437
  • 14
  • 21