Load JSON from assets and add it to JSONObject: Android - java

I need to store the user input and other string to a local storing method to use it later. Also I need to update it with data every time the user inputs other preferences and read it. I've chosen JSON for this task, hope it's the best option.
My problem is that every time I close the app (or update the app version) the file gets reset.
I would save the JSONObject to the internal storage and update, change, read and check for its existence every time the app starts.
This file is the core of my app as it stores the preferences.
UPDATE:
I've used SharedPreferences to do my task:
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

Ok, I've switched to using SharedPreferences. Thanks for your recommendations.

Related

How to keep track of information regarding fragments in Android

I have a fragment which is sort of like a test. The user can choose from a spinners menu if this test has passed, failed or undecided. Once the user leaves this fragment and then comes back to it later, I want the spinners menu to display what the user had previously selected.
Can anyone tell me how to keep track of this information? I am a beginner with Java and Android programming. Also, let me know if further explanation is required.
It probably depends on what scope you want to be able to refer to these tests in. For example, do you want these fragments to be emptied after the user closes the app, or not?
If it is just while in the app "session", you could just create an object in your fragment and override the onResume method to populate the spinners with the appropriate data.
If you want to save the info after the app is closed, there are a couple of options. For smaller sets of data you could use shared preferences
if you have an undetermined amount of "tests", I would advise saving whatever data you need to a file, and parsing it to fill the fields in the fragments
Take a look at this to get an idea of how to save data: http://developer.android.com/intl/es/training/basics/data-storage/files.html
I've done something similar to what your doing. I used shared preferences to save the string like so
Saving the string you got from the spinner in a fragment
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();
mSharedPreferencesEditor.putString("my_unique_key", myString);
// commit changes
mSharedPreferencesEditor.apply();
Remember to always apply the changes after you made them and you can save as many strings as you want as long as they each have a unique key.
Then to retrieve the value just
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String myStringValue = mSharedPreferences.getString("my_unique_key", null);
The second argument (null) will be returned if it could not find any data that had that unique key. (i.e if you haven't saved it yet)
You can also use another string instead of null like this
String myStringValue = mSharedPreferences.getString("my_unique_key", "could not find value");
or if you want to bundle a bunch of data together like say create an object to save a bunch of strings then save that object to a file I could show you that if you wanted to

How/where can I "stash"/keep data in my application?

I display dates to the user in a layout that are let's say are textual.
When the user presses a button I want to get the information in those fields that represent these "dates" but if I get the text in them is not of value to me.
I would need to store somewhere the original dates that created these "textual" elements and fetch them from there.
Is there a specific construct in android that one can use as a stash area or should I just use a static class with variable to hold them?
In your case, you should use SharedPreferences to store the data by converting it into a String (text) or int/long first.
This will allow you to easily write and retrieve data, and you should use this.
You can also use the file system to save almost any Java object using serializable, on Internal Storage.
Either way, the data will stay there even if your app is closed or the device is turned off.

Save input user in Edittex

Good morning, I need help,
I´m new using Java in Android development and need to save user input in a textbox without needing a button to trigger it.
Could someone give an example please?
You will need a TextWatcher, since you do not want to trigger a click event
Here! is a good example of TextWatcher control,
once you get families to TextWatcher, you can save your value either in a SharedPreference (Local tiny storage) or in a Databse, Refere This! for storing and retrieving values from SharedPreference

Best way to save a little amount of data

I'm currently developing an Android application where OAuth will be in use. In order to use OAuth, I need to be able to save the consumer's private key to access the protected resources. My question is, where would I want to save this tiny piece of data? I know that for most Android applications, data would be stored in a SQLite database. But for only a little bit of information, a string containing random letters and numbers, would SQLite be the best way to go? Or would it just be better to write the data to a file and save it on the file system? I don't know what the best practice for this would be, so hopefully I'll be able to get some insight.
Thanks a bunch guys!
You have a lot of choices:
http://developer.android.com/guide/topics/data/data-storage.html
However, if you just want to save one String (the OAuth key), I would suggest shared preferences. Example:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("oauthKey", yourOAuthKey);
// Commit the edits!
editor.commit();
I would save it in a file on the internal storage or in shared preferences.
Never the less you should encrypt the key for security reasons! On a rooted oder developer device it is no problem to access any file on the system.
SharedPrefs is much easier to work with than an sqlite database - especially for something so simple as you describe.
I guess a SharedPreference would do just fine.

Saving and Adding to A List (Android)

Plain and, hopefully simply...
-What I would like to do is make a list of strings.
-I would like to add to this list while in the application.
-Finally, I want to get each String from this list.
This must be saved somehow so that when you close it and open it back up, the list will save...
How should I get around to this? SharedPreferences? An SQL Database? What should I use to accomplish this?
I vote SharedPreferences since it will be faster and will persist throughout the life span of the application.
If you need to have the saved data made available during the next time the app is run, you will most likely want to use a DB

Categories