I have a json file. So, I was parsed the json file and I stored all value in an ArrayList
. Now, I want to share the value all the classes in application. So, what is recommended and efficient way to do that??

- 8,468
- 9
- 29
- 45
-
3@YasirAdnan Did you read any answers to the linked question? They apply to your situation. – Code-Apprentice Jul 03 '13 at 18:31
-
I suggest you look at [these suggestions from Google](http://developer.android.com/guide/faq/framework.html). – Code-Apprentice Jul 03 '13 at 18:33
-
@YasirAdnan In its current form , your question looks pretty similar to an already answered question. Go through it , if you find it unsuitable for your purpose , come back again and explain specifically . No need to sulk :) – AllTooSir Jul 03 '13 at 18:44
3 Answers
Singleton pattern.
just create a public static reference for your List
object and access it from any part of your app. it will be accessible as long as your app is alive. and you can deallocate it manually (by nulling the reference).
public class SharedData {
public static List<Custom> jsonData;
}
and from any part of your application:
SharedData.jsonData = new ArrayList<Custom>();
and to read the data
Custom obj = SharedData.jsonData.get(0);

- 9,192
- 5
- 39
- 51
-
I used this one is my main class public ArrayList
- data = new ArrayList
– Adnan Jul 03 '13 at 18:42- (); But when I use this activity.data.isempty() in another class. it return true. But, it shows false in main class.Any solution??
- data = new ArrayList
Now, I want to share the value all the classes in application. So, what is recommended and efficient way to do that??
Assuming that you missed out a preposition "with" in your above question and want to use your arraylist in all other classes as well, you might wanna go for a global Arraylist, which is nothing but declaring a public static Arraylist in a new class (let's call it GlobalState.java).
/** GLobalState.java **/
public class GlobalState {
public static ArrayList<E> jsonGlobalArrayList;
}
You can use getters & setters if you want.
Now, u can assign your arraylist to this global Arraylist as soon as u have populated your arraylist after parsing the json file.
GlobalState.jsonGlobalArrayList = jsonArrayList;
This is how I do it whenever I'm dealing with json values. Hope this helps.
P.S.: Excuse me for oversimplifying the solution this elaborately, as u might have understood this already when I mentioned about global ArrayList. :)

- 517
- 1
- 10
- 23
public class Utilities { public static boolean boolVar = false ; public static int intVar= 0 ; public static String str = "" ; public static List<Object> aList= new ArrayList<Object>() ; public static HashMap<String, Object> aMap= new HashMap<String, Object>() ; }
Use them by using class name like;
Utilities.boolVar = boolValue ; Utilities.intVar = intValue ;
inside activities.

- 466
- 6
- 16