how to save arraylist after the app is closed. This is my global class
public class GlobalClass extends Application {
ArrayList<Trip> allTrips = new ArrayList<>();
public ArrayList<String> allTripsString() {
ArrayList<String> allTripsString = new ArrayList<String>();
for (Trip trip : allTrips){
allTripsString.add(trip.getName());
}
return allTripsString;
}
}
This is my trip class
public class Trip {
/** A map with category as key and the associed list of items as value */
Map<String,List<Item>> expanses;
private String name;
public ArrayList<String> ExpensesCategory = new ArrayList<>();
public ArrayList<String> Adults = new ArrayList<>();
public ArrayList<String> Children = new ArrayList<>();
public ArrayList<String> ShoppingName = new ArrayList<>();
public ArrayList<Double> ShoppingPrice = new ArrayList<>();
public ArrayList<String> ShoppingCategory = new ArrayList<>();
public double budget = 0;
/** An item in the expanses list */
static class Item {
final String name;
final double cost;
final String Category;
public Item(String name, double cost, String Category) {
this.name = name;
this.cost = cost;
this.Category = Category;
}
@Override public String toString() {
return this.name + " (" + this.cost + "$)";
}
public String getName(){
return name;
}
public String getCategory(){
return Category;
}
public double getCost(){
return cost;
}
}
public Trip(String name) {
this.name = name;
this.expanses = new HashMap<String,List<Item>>();
for (String cat: ExpensesCategory) { // init the categories with empty lists
this.expanses.put(cat, new ArrayList<Item>());
}
}
public String getName(){
return name;
}
/** Register a new expanse to the trip. */
public void add(String item, double cost, String category) {
List<Item> list = this.expanses.get(category);
if (list == null)
throw new IllegalArgumentException("Category '"+category+"' does not exist.");
list.add( new Item(item, cost, category) );
}
/** Get the expanses, given a category.
* @return a fresh ArrayList containing the category elements, or null if the category does not exists
*/
public List<Item> getItems(String category) {
List<Item> list = this.expanses.get(category);
if (list == null)
return null;
return new ArrayList<Item>(list);
}
/** Get the expanses, given a category.
* @return a fresh ArrayList containing all the elements
*/
public List<Item> getItems() {
List<Item> list = new ArrayList<Item>();
for (List<Item> l: this.expanses.values()) // fill with each category items
list.addAll(l);
return list;
}
public ArrayList<String> getItemsString() {
List<Item> list = new ArrayList<Item>();
for (List<Item> l: this.expanses.values()) // fill with each category items
list.addAll(l);
ArrayList<String> listString = new ArrayList<String>();
for (Item item : list){
listString.add(item.getName());
}
return listString;
}
public ArrayList<Double> getItemsCost(){
List<Item> list = new ArrayList<Item>();
for (List<Item> l: this.expanses.values()) // fill with each category items
list.addAll(l);
ArrayList<Double> listDouble = new ArrayList<>();
for (Item item : list){
listDouble.add(item.getCost());
}
return listDouble;
}
public ArrayList<String> getItemsCategory() {
List<Item> list = new ArrayList<Item>();
for (List<Item> l: this.expanses.values()) // fill with each category items
list.addAll(l);
ArrayList<String> listString = new ArrayList<String>();
for (Item item : list){
listString.add(item.getCategory());
}
return listString;
}
/** Get the total cost, given a category. */
public double getCost(String category) {
List<Item> list = this.expanses.get(category);
if (list == null)
return -1;
double cost = 0;
for (Item item: list)
cost += item.cost;
return cost;
}
/** Get the total cost. */
public double getCost() {
double cost = 0;
for (List<Item> l: this.expanses.values())
for (Item item: l)
cost += item.cost;
cost *= 1000;
cost = (int)(cost);
cost /= 1000;
return cost;
}
}
I want to save the array list and when I close and open the app the array list (alltrips) is saved. How to do this?
I want to use shared preferences but I don't know how to do this because it is not a string it array list with Trip. Can anyone help me to save the arraylist in shared preferences?