I am working with Android Studio 2.2.3 in order to watch data received from a bluetooth module in my smartphone's screen. I can see these values in my activity but as I am receiving values each 10 seconds (for example) I would like to save all this data in an array, database or anyplace. Once it was saved I would like to build a graphic using this saved data.
Here I have a picture of what I receive:
enter image description here
So now I would like to save those values (53,54,55...) and I don't know how. Is there any way like making an ArrayList or some function to save it? I read about "SharedPreferences" but I think it is not designed for what I am looking for.
Thank you very much!
P.D: I hope I explained okay and sorry for my english.
Shared Preferences could be, it allow to use Sets. You could convert your List into a HashSet or something similar and store it like that. When your read it back, convert it into an ArrayList.
//Retrieve the values
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Set<String> set = prefs .getStringSet("key", null);
yourListOfStrings.addAll(set);
//Set the values
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Set<String> set = new HashSet<String>();
set.addAll(yourListOfStrings);
prefs.putStringSet("key", set);
prefs.commit();
Regards,
Related
I want to create a game that counts the clicks made in 60 seconds and saves the record. I would like this record to be saved on the device even after the app is closed. Is there some kind of variable that allows me to do this?
Android's Shared Preferences seem to be the most relevant option for you.
Refer to the official documentation for an in depth look:
https://developer.android.com/training/data-storage/shared-preferences
These code samples should help you as well:
To save a value into the Shared Preferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key); //set a default/backup option
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);
Make sure to keep the key identical between placing values into the preferences and retrieving.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key);
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);
SharedPreferences is probably the place to start with this. You can view Android documentation related to it here along with code examples: Save key-value data
There are some other options for saving data in an Android app on the left pane of that page, but SharedPreferences is probably the most applicable based on your described use case.
I have a school planer app and I want to save the classes that are written in an EditText object. The problem is, the EditTexts are empty when I close the app and then open it again, and even when I press back. This may be a noob question, but I'm a beginner and I need help. Please answer quickly...
for saving edittext text you need to store the text into storage.
android support storage like
Database
Shared Preferences
For your problem you need to store your Edittext text into the storage when you change value.
And when you open it again you need to retrieve from the storage.
For you best option will be shared preferences. For more Go with This Example . This will make you understand save and retrieve shared preferences.
Short Example
Store Data
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
editor = pref.edit();
editor.putString("first_edittext", "value");
editor.commit();
Retrieve Data
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
pref.getString("first_edittext", "");
I'm trying to develop a simple notepad on android. But I don't know how to save my notes(strings) to internal storage(or to an SQL database if it's faster). if I used internal storage would I be able to save a couple of strings and get them back? I'm a beginner to mobile application development and this is my first project. so I'd really appreciate it if you could show me a sample code so I can learn from it. Thanks!
A database is an option, therefore you'll definitively have to read the follow page, that helped me a lot. There is also some sample code in it.
http://www.vogella.com/articles/AndroidSQLite/article.html
In paragraph 9.7 is the full code for adding, editing and deleting records...
An other option is saving the string in an .txt file and save that on the storage. Than this might bring you further:
Write a file in external storage in Android
Good luck!
You can save it in shared preference if it is not too big.
To store:
SharedPreferences sharedPref = getSharedPreferences("SomeName", Context.MODE_PRIVATE);
Editor editor = sharedPref.edit();
editor.putString("String1", value); // value is the string you want to save
editor.commit()
To retrieve:
SharedPreferences sharedPref = getSharedPreferences("SomeName", Context.MODE_PRIVATE);
String retrievedString = sharedPref.getString("String1", defaultValue);
"SomeName" ---- the preference name
"String1" ---- key for the string you want to store/retrieve
defaultValue ---- in case the key is not available, this is the retrieved string
I am developing an android Application for managing and creating personal and business cards.
How can I use Shared Preferences (where i will be storing my personal and professional information ) in creating the cards.
Every time i need to create a new card, the program should ask me to gather information from the Shared Preferences and also my "create new Card" form should ask for adding new fields (like another or new e-mail not present in shared preferences ) dynamically.
please let me know any sample examples explaining such scenario..... I have extensively searched over internet to find some, but couldn't get any as per my requirement.
Thankyou.
For your requirement it should be better to use sqlite database. because if you are adding more records then it always better to use database for storing similar type of data..
otherwise if you want to use Shared Preference only then try like this...
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
//Storing data
Editor edit = preferences.edit();
edit.putString("pref_empId", _empid);
edit.putString("pref_userName", _username);
edit.putString("pref_userType", _usertype);
edit.commit();
//Retrieving
pref_userName = preferences.getString("pref_userName", "n/a");
pref_empId = preferences.getString("pref_empId","n/a");
pref_userType = preferences.getString("pref_userType","n/a");
In shared preferences you can store the info as key-value pair. For details read http://developer.android.com/guide/topics/data/data-storage.html#pref
I'd like my app to allow a single user to create a profile that is accessed every time they run the app. The user's profile contains a String entry and five int entries. I've thought about using an SQLite database to store the info between sessions, but it seems like overkill since I'd only need one row in a single table to store the data. However, I'm not sure if it's possible to use SharedPreferences on a String type of data.
Can anyone provide some insight into this issue please? Thank you!
Yes you can use SharedPreferences.
To write
Editor editor = getSharedPreferences("file_name", MODE_PRIVATE).edit();
editor.clear();
editor.putString("pref_name", myStringValue);
editor.commit();
To read back
SharedPreferences preferences = getSharedPreferences("file_name", MODE_PRIVATE);
String myStringValue = preferences.getString("pref_name", "default_value");
Regards.