1

Im new to java and I saw this link here and my questions is pretty similar, but I coudln't understand how that works.

I have a ArrayList with a few variables, how I can fill this variables and pass it to a new activity? My new activity is named as Detail.java

Im creating my ArrayList as

public final List<Car> ExtraCars = new ArrayList<Car>();

so I can use in the next activity, Im not sure if is this the best way to use.

The structure of this Car is:

public Car (String make, int year, int iconID, String condition){
    super();
    this.make = make;
    this.year = year;
    this.iconID = iconID;
    this.condition = condition;
}

I would like to know how to fill this ExtraCars with some random variables, like make = actualmake; and all this be passed to the Detail.java

And also to retrieve this informations on Detail.java should I do this?

Intent intent = getIntent();
String make = intent.getStringExtra(MainActivity.ExtraCars.make);
Community
  • 1
  • 1
Guizinhobeback
  • 86
  • 1
  • 14
  • see if this helps you: http://stackoverflow.com/questions/21732520/what-happens-to-an-arraylist-in-an-activity-that-is-recalled/21733769#21733769 – pedromss Feb 14 '14 at 00:43
  • Thanks, I gonna read this – Guizinhobeback Feb 14 '14 at 00:44
  • just declare a public function in the class that takes it in the params. Or pass it in the extras of the intent. or a million other ways. You are overthinking this. An Activity is still a class. It just depends on how and when you need the info for how you build it. – WIllJBD Feb 14 '14 at 00:45

2 Answers2

2

I would avoid declaring anything public like suggested by daentech as this bypasses the much acclaimed encapsulation and data protection mechanisms of Java. Instead try pedromss' link. Also...

Bundles is another option, have a look at this: https://stackoverflow.com/a/6681784/3199478

Community
  • 1
  • 1
codehitman
  • 1,148
  • 11
  • 33
-1

As the post you linked states, you could make it a public static variable:

public static List<Car> ExtraCars = new ArrayList<Car>();

In order to retrieve the data in Detail.java you don't need to getStringExtra, instead just use:

String make = MainActivity.ExtraCars.get(index).make;

You don't then need to pass the ArrayList in the intent

This however doesn't follow best practices for java

daentech
  • 1,125
  • 12
  • 16
  • Terrible implementation, violates some of the most valuable principles of OOP, increases coupling, error prone, low cohesion, and do not encapsulate... – Martin Cazares Feb 14 '14 at 00:56
  • The answer here http://stackoverflow.com/a/5567079/4739 suggests the same, and references the Android docs stating a public static field. – daentech Feb 14 '14 at 01:01
  • If you look a little more to the bottom of the list, there's an answer with twice more upvotes than the actual accepted one, that's the proper way to do it, having statics referenced this way is the first step of crappy code... – Martin Cazares Feb 14 '14 at 01:03
  • My apologies. That is a much better way of doing it :) – daentech Feb 14 '14 at 01:06
  • Thanks daentech, totally worth it, I know that have some restrictions about private and public, but how I'm not too familiar with java, this is not a issue for me, and I hope its not one for for my teacher too. Many thanks! – Guizinhobeback Feb 14 '14 at 01:21
  • Im sorry, daentech, Im receiving this error message in Detail.java "index cannot be resolved to a variable." What am I supposed to do? – Guizinhobeback Feb 14 '14 at 01:49
  • index is the position of your car object in the arraylist which you want to display on your details page. The arraylist ExtraCars will hold all of your cars. If you are just wanting one object for the details page I would look into codehitman's link or this one: http://stackoverflow.com/a/2141166/4739 and add your Car object to the bundle to read out in the details page – daentech Feb 14 '14 at 01:55