0

I have this method which is used to store a list of Applications.

public void storeApplication(String name, String item){
    peopleAttending.add(new Application(name, item));
}

It does what it's supposed to do, however when the method finishes, the data contained within the ArrayList also gets destroyed.

I've declared and instantiated the ArrayList globally:

private ArrayList<Application> peopleAttending = new ArrayList<>();

So I would have thought that once the object is added to the ArrayList, it would be safely stored until the program terminates.

Any help would be greatly appreciated.

Class that calls storeApplication:

    public void saveBookingInfo(View view) {

        GuestsAttending sendApplication = new GuestsAttending();

        EditText applicantNameText = (EditText) findViewById(R.id.applicantNameTextField);
        EditText itemToBurnText = (EditText) findViewById(R.id.itemToBurnTextField);

        String appName = applicantNameText.getText().toString();
        String appItemToBurn = itemToBurnText.getText().toString();

        if (appItemToBurn.isEmpty() || appName.isEmpty()) {
            Toast.makeText(BookingScreen.this, "Please fill in all fields.", Toast.LENGTH_SHORT).show();
        }
        else {
            sendApplication.storeApplication(appName, appItemToBurn);
        }
    }

Edit - Entire class:

public class GuestsAttending extends Activity {

    private ArrayList<Application> peopleAttending = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guests_attending);
    }

    public void storeApplication(String name, String item){
        peopleAttending.add(new Application(name, item));
    }
}
Melquiades
  • 8,496
  • 1
  • 31
  • 46
  • 1
    can you share your code – Saurabh Jhunjhunwala Apr 22 '15 at 10:39
  • Create `Singlton Class` and store in it. – M D Apr 22 '15 at 10:39
  • 1
    It would be help us to see the whole (or maybe just the relevant parts of the) class containing `storeApplication` and `peopleAttending`, as well as the code that calls `storeApplication`. – Philipp Matthias Schäfer Apr 22 '15 at 10:40
  • you probably create a new instance and want to read from the already populated ArrayList .. that's not going to work if so – 12dollar Apr 22 '15 at 10:40
  • Not much context here, as an initial guess I'd check that your'e querying the list from the same object that you called storeApplication on. – Anuj Shah Apr 22 '15 at 10:41
  • We need to see the whole code. Objects are garbage collected after such time when nothing on the stack references them anymore. If nothing is maintaining a reference to this class then yes, you'll lose the data.A singleton class as suggested by @SaurabhJhunjhunwala would be a step in the right direction. But hard to say from this small snippet. – James Lucas Apr 22 '15 at 10:42
  • Are you creating GuestsAttending instances often? Well, more than once even. The arry list is a member field so every time you create one you'll get a new instance. – James Lucas Apr 22 '15 at 10:44
  • Where do you access the `peopleAttending` list to read its content? – Tom Apr 22 '15 at 10:46
  • @Tom I've been debugging it at the moment to reads it contents, I've also got a Toast.makeText() giving me an insight of what's in the list. –  Apr 22 '15 at 10:48

3 Answers3

0

try to modify your code, a bit.

  1. private ArrayList peopleAttending;
  2. have getters and setters
  3. before adding to list check if(peopleAttending==null) peopleAttending = new ArrayList<>();
  4. in the calling method, create and obj of GuestsAttending and then try getting the list using the getter.

As per your request, will elaborate my point:

in GuestsAttending you are using

private ArrayList<Application> peopleAttending = new ArrayList<>();

I want you to break this line and have

private ArrayList<Application> peopleAttending;

public ArrayList getPeopleAttending(){ 
    return peopleAttending;
}

public void setPeopleAttending(ArrayList<Application> peopleAttending){ 
    this.peopleAttending=peopleAttending;
}


public void storeApplication(String name, String item){
    if(peopleAttending==null) 
        peopleAttending = new ArrayList<>();

    peopleAttending.add(new Application(name, item));
}

Assuming you are going to use the peopleAttending list in saveBookingInfo class, you should use

ArrayList<Application> peopleAttendingLst = sendApplication.getPeopleAttending();

You should be able to get the objects in the list.

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • Could you clarify a bit more please? (Write example methods and declarations). Thanks! :D –  Apr 22 '15 at 10:57
0

Your GuestsAttending extends Activity class, hence DO NOT THIS:

GuestsAttending sendApplication = new GuestsAttending();  //remove this line

Let the system deal with creating activity.

I'm guessing saveBookingInfo() could also be a member of GuestsAttending activity, which get's wired eg. to a button.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
  • Further down: sendApplication is used to externally call the storeApplication method, what do I put in its place? -- saveBooking info is wired to a button within a related XML file to the Booking Screen class. –  Apr 22 '15 at 10:59
  • It's not clear how you structure your app. Which activity calls saveBookingInfo() ? – Melquiades Apr 22 '15 at 11:04
  • I have an xml file with two text fields and a button. The saveBookingInfo() method is called from the button, which is used to convert the text fields into variables and pass them through the external method. –  Apr 22 '15 at 11:16
  • Ok, which activity does saveBookingInfo() belong to? What are the relationships between your activities? Why do you call external method to save, not inside current activity? Or in general, how does user navigate between your activities and what/where you want them to do? – Melquiades Apr 22 '15 at 11:36
  • There are two activies to this program: One to collect the information (Booking screen), and one to store the information, which will be displayed on another activity. Sort of like a: "Who's attending the event?" type of system. Once the information has been collected from the booking screen I need to move it to the display screen so that the information can be used there. The user navigates through the use of a main menu, which contains several intents depending on where the user wants to go. The saveBookingInfo belongs to the booking screen activity. :) –  Apr 22 '15 at 11:41
  • There are a couple ways to accomplish what you want: you can store saved data in SharedPreferences, in SQLite database, other activities can receive this info through an intent, or read from the mentioned places. Main point though is to **save your data in Booking activity, then if you want to display it later, read from the source where you saved it to, or receive via an intent from booking activity.** – Melquiades Apr 22 '15 at 11:45
  • could you provided a method example of how I would achieve this? (The receive via an intent from the booking activity); –  Apr 22 '15 at 13:06
  • Not to duplicate posts, here are two: http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android and http://stackoverflow.com/questions/19286970/using-intents-to-pass-data-between-activities-in-android – Melquiades Apr 22 '15 at 13:14
0

I would have thought that once the object is added to the ArrayList, it would be safely stored until the program terminates

Nope. Looking at the posted code saveBookingInfo instantiates GuestsAttending which declares peopleAttending. When saveBookingInfo completes GuestsAttending is destroyed hence peopleAttending is destroyed.

Declare peopleAttending in your first activity and use intents to pass around other screens. Don't forget to use onSaveInstanceState to deal with App closures by the OS.

Daniel S. Fowler
  • 1,982
  • 15
  • 12