1

I am developing my first android Application and hoping to get some tips here.

I am getting a JSONObject from an url which then will be parsed in an ArrayList<MyObject>. The list will be used in multiple tabs and be filtered as needed for the tabs. The objects within these list can be modified by the user and the changes should be synchronized with the lists.

So, to speed up loading time I have created a class DataHolder as a singleton which contains 7 arraylist, based from the one JSONObject in different sorting order and filter criterion. The objects in these lists are references from the original list. Populating the lists works fine.

The lists will be used in different fragments and activities.

Now the problem: the second activity contains tabs with fragments. After initializing the fragment... all arraylists in the DataHolder counts 0! I have to save the JSONObject in SharedPreferences and populate it again to get the List. I can't load the url again because it is slowing down the app to much and using SharedPreferences is not an option (I think) because of the need to synchronized the Lists. I have read that using static variables is not the optimal solution, but it seems to be the easiest way :(

What can I do to solve this problem? Should I use Parcelable Objects and always pass the Lists around? Or maybe use SQLite? Or are there other approaches?

Niko
  • 26,516
  • 9
  • 93
  • 110
Diana
  • 43
  • 6
  • It's not clear how you've done this - is your `DataHolder` a POJO or did you extend `Activity` or is it an inner static class of the first `Activity`? If it's either of the second two then this definitely not what you should do. An `Activity` should never expose static methods or member variables or inner classes of an `Activity`. – Squonk Aug 23 '12 at 19:48
  • create an `Application` class and put your data inside the instance (you can assign the instance in `onCreate()`) http://developer.android.com/reference/android/app/Application.html it needs to be declared also in the manifest – sherpya Aug 23 '12 at 21:38
  • My DataHolder is a POJO. I have tested extending the application with the same results. After the Fragments are used, the list is 0 :(. – Diana Aug 24 '12 at 11:23

2 Answers2

0

use static list for local operation .when user comes out of that screen(activity) save that changes into database.

chet's
  • 193
  • 2
  • 8
  • Why would you need to use `static` members for an `Activity` if it's only going to be used 'locally'? – Squonk Aug 23 '12 at 19:52
0

The SQLite way is definitively the correct approach, imho.

You should use the internal database to store such lists : http://developer.android.com/guide/topics/data/data-storage.html#db

Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • The two main options I was thinking about now are using sqllite or sharedprefs to save the lists. I need to update all my listviews synchronious if changes are made from the user. Using sqllite is maybe more cleaner. I will try this. Thanks! – Diana Aug 24 '12 at 04:16