2

I currently click a button from MainActivity which takes me to HotSpotsActivity, displaying a list of sports team names in alphabetical order. Upon pressing Back to go to the MainActivity (and even if I hit home and resume the app), HotSpotsActivity redownloads the data and displays it below the original items, so I will have as follows: Team A, B, C, D, E, Team A, B, C, D, E.

If I back up and try again, it displays a 3rd copy, then a 4th, and so on.

The confusing thing is that I already have a related activity (EventsActivity) that has no issue with this, and has a clear view when I resume the EventsActivity and then redownloads the data. All of the code relating to the downloading of data is nearly identical, can someone please tell me what I am missing?

I have included the EventsActivity and related EventsAdapter, both of which work, and the HotSpotsActivity and related HotSpotsAdapter, which do not work.

DOES WORK: http://pastebin.com/mG5FVnbx http://pastebin.com/aXEw9T8S

DO NOT WORK: http://pastebin.com/FNPKyisB http://pastebin.com/F00w4gUn

2
  • have you tried calling finish() before starting the HotSpotsActivity?
    – bman
    Sep 28, 2013 at 23:27
  • My problem isn't with my Mainactivity, the duplication is occurring within HotSpotsActivity. Also, I don't call finish at all in EventsActivity, so I am really looking for the cause I'd my problem in my current code.
    – mattcoker
    Sep 28, 2013 at 23:43

2 Answers 2

2

Your arraylist in hotspots is static. This means it exists constantly and you are never calling clear(). So when you call add() in the json parsing you are just appending over and over again. It looks like your other array in events is not static so it is a new array every time you create your activity. Hope this helps :)

1
  • Thank you! Works like a charm! Still pretty new at this, only been doing it about 2 months in my spare time.
    – mattcoker
    Sep 29, 2013 at 0:45
0

You are most likely adding the items when the activity starts, even when they are already in the listview. Check your code.

1
  • I just don't understand why that would happen when I reused a ton of the code from the working activity. Do you see anything in the code? Because I was under the impression that any code in the "onCreate" section would overwrite anything there before.
    – mattcoker
    Sep 28, 2013 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.