Is it possible to auto-increment getSharedPreferences name? - java

I am a novice android programmer and am creating an app where the user can fill out a form multiple times and, later, pick from a list showing the previous entries.
The call function for sharedprefences is: SharedPreferences getSharedPreferences (String name, int mode);
One idea I had was to have the user fill out a title and have that be String name but then I wouldn't know how to list them. So I think an array would be best that could manage itself. This would allow me to list each form using a for loop from i = 0 to i = lastFormFilled
Any ideas?

Related

What is the easiest way to save an ArrayList in Android?

I'm building an app that has a favourites list, but when I exit the app, the favourites list is reset to nothing because the list is not being saved (since lists can't be saved in SharedPrefs). How can I save an ArrayList OR String[] in Android? If you recommend Serialization or a Local Database, could you please explain what these are and mean? I've heard the terms and followed tutorials but do not understand.
Maybe another approach to the same question is how would you normally build a favourites list?
There's a question that explains it in a very nice way:
Save ArrayList to SharedPreferences
From its answer (I've already tested it), after API 11 you can do something like this:
//Retrieve the values
Set<String> set = myScores.getStringSet("key", null);
//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();
I summarized his answer, but you should check the complete answer. It's really helpful!
Option1: easiest. Iterate over arraylist and save each element into sharedPreferences
option2: convert arraylist into a string. Then save into shared pref.
option3: save your data into a database. Google how to use sqllite with android
You can use Sugar ORM library for Android. It will create for you the database you need by simply making the object class of your ArrayList to extend SugarRecord.
For example, if your List type is ArrayList you have to make your Books class extend SugarRecord, and then in your Activity iterate in a loop through the items and set item.save() and it is saved to the database.
I have a sample project here.
If you have any question send me a PM.
Hope it helps!

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.

JTextArea Storing Each Line

I'm making an Address Book application, and I have the basic fields (name, age, phone numbers, etc.) and I have a JTextArea for placing miscellaneous comments, since I won't know how much info I need to add. It could be one line or hundreds.
I want to know if there is a way to store this data and place it back into the field when the contacts information is loaded. I could store the entire JTextArea as a String, but would that keep the correct formatting? Or save many Strings into an ArrayList or something similar.
Additionally, is there a method to save blocks of text as opposed to single lines? For example. I might want to store an URL, or I might want to store a 5-line poem the contact wrote. Obviously, storing the URL could take one line, but the poem would take 5 lines and therefore 5 Strings.
What seems like a decant storage solution? If my concept doesn't make sense, please comment and I'll try to make it more clear.
Saving it as a single String should fine, it will, normally, preserve newlines as well.
This will of course, depend on where you are storing it
It sounds to me like your address book app is asking a user to input simple values as you suggested and I'm assuming it is saving these values into a "Contact" object or something similar.
If this is the case, you can simply add a String comments; to the Contacts class and implement a getter and setter method accordingly.
Just get the data from the JTextField and store it into that Contact's comments variable.
Contact person = new Contact();
person.setComments(textArea.getText());
Something like that. Then you can reference the person.getComments() later on and print that back out to the TextField if thats what you want to do with it.

Submitting variable length forms based on user input

I'm trying to wrap my head around this. My language is Java using PlayFramework but I think any answer using php, or ruby on rails would get me in the right direction. Basically the user can add or take away number of pallets in this form. There needs to be some javavscript calculations per tab also. Everything is fired using a input change jquery event so there is no submit buttons. My first mockup used UUID's but that seemed to get me nowhere.
What happens is each tab is reponsible for doing calculations like perimeter. My first question is how to call javascript for the specific tab. I first did it with UUID's and separate scripts for each tab. This didn't seem correct.
My second problem is submitting this data. Obviously the name parameters need to be different. So I'm scratching my head on this. Every pallet would be tagged with an id so I'm not worried about the backend as much as I'm trying to figure out how structure the front end and the controllers.
Any help would be great. Thanks.
On the javascript part, assuming all the tabs have the same structure, create a generic method that receives and id (id of the tab) as parameter, and then retrieves the proper fields form the calculation. Something like:
function doCalc(tabId){ //assumes jquery
var tab = $("#"+tabId);
var field1 = $("#"+tabId+"_field1");
var field2 = $("#"+tabId+"_field2");
//...etc to retrieve tabs
}
The method assumes that when you create a tab "tabId" and its elements, you generate the id of the elements with the formula "tabId_", which will facilitate finding them.
About the submission, using Play! you should check this section about Ajax requests. It could be as simple as doing the calculation (the method above) and at the end call a Play method via Ajax. This method would get all the parameters (id, values, etc) of a pallet (the pallet of the given tab) and save it.
For submission, I assume you are trying to figure out how to hold and pass the data in a bean. What about having a field as below:
LinkedHashMap<String, LinkedHashMap<String, Object>> field;
The inner maps would be the listing of the fields on a Pallet. The outter maps are the Pallets.

Categories