0

I am very new to java and am making some assumptions based on what I have read, so please correct me if I am wrong.

I'm making an Android app that downloads JSON data from the SD card, which you can then search by filtering.

If I were doing this in javascript, I'd just save the data to global variable and access it whenever. But (from the little I have read), java doesn't have globals and what it does have that are similar aren't really wise to use.

So what I do now is that every time a new search begins, is I download all the data again from the SD card. But this seems horribly inefficient. I have read a little bit about Singletons and Builders and I guess maybe that's what I'm looking for (I'm really stumbling around in the dark here), but I don't really understand how to implement them in this case, or if they are in fact the best solution.

If it makes any difference, the data is never going to change while the app is running.

Could somebody give me some pointers on the best way to do this? I really just want to get the data once when the app loads, and then be able to access it whenever later. If it helps, this is the code that I use to get the data from the SD card...

public static JSONArray getArray(){

    FileInputStream stream = null;

    String jString = null;
    File yourFile = new File("/mnt/extSdCard/app/thejson.json");

    try {
        stream = new FileInputStream(yourFile);
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        jString = Charset.defaultCharset().decode(bb).toString();

        } catch (FileNotFoundException e) {
        Log.i("err", "file not found");
    }
    catch (IOException e) {
        Log.i("err", "IO exception");
    }
    try {
        stream.close();
        } catch (IOException e) {
        Log.i("1", "IO exception");       
    }
    try {
        names = new JSONArray(jString);
        } catch (JSONException e1) {
            Log.i("err", "IO exception");
    }
    return names;
}     
lucas
  • 1,485
  • 13
  • 22
  • 1
    What makes you think Java doesn't have global variables? Just make a static object in one of your classes and access it with `MyClass.myObject`. – BLuFeNiX May 16 '13 at 17:40
  • Pointers ---> http://developer.android.com/guide/topics/data/data-storage.html – petey May 16 '13 at 17:47
  • I'm picking this up on the fly, so half-understanding what I'm reading - I guess when people said in [this thread](http://stackoverflow.com/questions/4646577/global-variables-in-java) that there are no globals in java, I kind of took it literally. Thanks for the replies. – lucas May 16 '13 at 18:35

2 Answers2

1

You can save your data in a few different ways in Java. If you want to share data amongst many different classes, you can create a variable in one class and call it public, or you can create a public static return method which returns the desired value (in this case the variable is private). A static method can be accessed regardless of whether a class contains the object which contains the method. Another way to store your data, although I do not recommend this, I recommend the static method, is to create an interface and have all classes using the data implement that interface.

jaredad7
  • 998
  • 2
  • 11
  • 33
  • Note, this is not persistent data storage, but you only need persistent storage if you want to save the data even after your app is completely closed, which is unnecessary. – jaredad7 May 16 '13 at 17:51
  • Thanks. I ended up declaring `public static JSONArray names;` at the start of my splashscreen activity, then `final JSONArray names;` at the end - seems to work OK - if there are any drawbacks, I'm all ears. – lucas May 16 '13 at 18:37
  • Just out of curiosity, what are you using the final array for? Also, could you set my answer as the Accepted Answer if it's the solution you used? – jaredad7 May 16 '13 at 18:43
  • the final array is used as base data - another class then grabs that and filters it according to user input before returning the results. Answer accepted - sorry, also a bit of an SO newbie – lucas May 17 '13 at 04:16
0

Just make your "names" JSON Array a member variable in your main activity and you can access it as long as your activity lives.