Reputation:
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));
}
}
Upvotes: 0
Views: 95
Reputation: 2922
try to modify your code, a bit.
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.
Upvotes: 0
Reputation: 2033
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.
Upvotes: 0
Reputation: 8598
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.
Upvotes: 0