Remixt Remixt - 12 days ago 12
Android Question

Why is my Array List Printing incorrectly the second time?

Is there something about Array List properties that I'm missing here?

I'm adding a list of custom objects to my list, each one with a unique name.

When I access the list a second time, every object in the list is the same. There is no code between accesses when I tested this.

// Add all the Tastr Items to the Array list using the list of restaurants found by yelp.
for (int i = 0; i < yelp.getRestaurants().size(); i++){
TastrItem temp = new TastrItem();
temp.setRestaurant(yelp.getRestaurants().get(i));
itemList.add(temp);
System.out.println("First Time Accessing the list --- > " + itemList.get(i).getRestaurant());
}

for (int i = 0; i < yelp.getRestaurants().size(); i++) {
System.err.println("Second time Accessing the list --- > " + itemList.get(i).getRestaurant());
}


Output:

I/System.out: First Time Accessing the list --- > Stanton's City Bites
I/System.out: First Time Accessing the list --- > Aladdin Mediterranean Cuisine
I/System.out: First Time Accessing the list --- > The Breakfast Klub
I/System.out: First Time Accessing the list --- > M&M Grill
I/System.out: First Time Accessing the list --- > Big & Juicy Juice Bar
I/System.out: First Time Accessing the list --- > Eddie V's Prime Seafood
I/System.out: First Time Accessing the list --- > Baby Barnaby's Café
I/System.out: First Time Accessing the list --- > Pappas Bros Steakhouse
I/System.out: First Time Accessing the list --- > Just Dinner
I/System.out: First Time Accessing the list --- > Green Seed Vegan
I/System.out: First Time Accessing the list --- > Local Foods
I/System.out: First Time Accessing the list --- > Salad Extraveganza
I/System.out: First Time Accessing the list --- > Luigi's Pizzeria
I/System.out: First Time Accessing the list --- > Houston's Restaurant
I/System.out: First Time Accessing the list --- > Hugo's
I/System.out: First Time Accessing the list --- > Huynh Restaurant
I/System.out: First Time Accessing the list --- > MKT BAR
I/System.out: First Time Accessing the list --- > Reef
I/System.out: First Time Accessing the list --- > Mockingbird Bistro
I/System.out: First Time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese


Edit: The answer is that the Restaurant variable was static in the Tastr Item Class.

Answer

I assume that in the TastrItem class, you are saving the item to a static variable. Therefore, though you are correctly creating a new TastrItem in each loop, the static variable is always set to the last value. To solve this, just edit the TastrItem class, and remove the static keyword.