I am using Android Studio and programming in Kotlin. I haven't been able to find a way to save the data entered by the user so it appears when they reopen the app. Thank you ahead of time!
You have to store your data in shared preference when any click event listen or on edittext textwatcher you can set your character in preference but best way is on any click event save data on preference and when activity open set data from preference on edittext.
Store it in Shared Preferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
use this save the value..
Editor editor = sharedpreferences.edit();
editor.putString("key_name", "value"); //here give name and value to save
editor.commit();
use this to get the value.
String edittextvalue = pref.getString("key_name", null); //put edittextvalue where ever you what to show the value.
use this step to remove or clear the value
editor.remove("name");
editor.commit();
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,
I tried to append the data in shared preference file by using
SharedPreferences sharedPreferences = getSharedPreferences("myData", MODE_APPEND);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", userName.getText().toString());
editor.putString("password", password.getText().toString());
editor.commit();
But I found that new value overwrites the old value. Will you help me to fix this issue?
MODE_APPEND doesn't mean that you add multiple values for each key. It means that if the file already exists it is appended to and not erased . We usually used MODE_PRIVATE.
As for saving multiple names and passwords, you can take a look at putStringSet(string key Set<String> values Method.
You can save the for each key a set of string values. You can separate the username and password by some special character or string. You may even serialize an object to json.
So basically what you need to do is:
Get the list of values from Shared Preferences
Append the current value to the list.
Save the List back to Shared Preferences.
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", "");
On my app I've got a Database which i have to fill one time with some entries. I have packed this step in my splashscreen. Now I got the problem every time i open up my application it puts again all entries to my database.
I'm looking for a way to let this happen only on the really first time the app gets opend. But i don't find out, how i can realize this.
I'm happy over every input and Idea!
Have a nice day
safari
Notes
I had an idea to realize that. I make a boolean in my SplashScreen, which takes a look at the database table if there is nothin, it inputs all the entries and makes a mark in a table that its filled like yes.
Afterwards if i open the app again it takes a look at the database if theres a yes or what ever i definied, it doesn't put again all entries in my database.
Sorry for that crappy definition of what my idea is, my english isnt the best.
I'm searching for an easier way than this.
why not have a startup activity that checks if the app is launched for the first time ( and stores the value in the app preferences ) and either forwards to the database setup activity or to the main activity....
Here's a sample on how to read and write to the preferences:
Define preference key:
public static final String KEY_APP_IS_INITIALIZED = "APP_INITIALIZED";
read boolean value from preferences:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context)
boolean appIsInitialized = prefs.getBoolean(KEY_APP_IS_INITIALIZED, false);
store boolean value to preferences:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context)
Editor editor = prefs.edit();
editor.putBoolean(KEY_APP_IS_INITIALIZED, true);
editor.commit();
Launching the activities:
Intent intent = new Intent(startup_activity,
db_initialized ?
MainActivity.class :
DbInitActivity.class);
startup_activity.startActivity(intent);
You could use SharedPreferences to store a boolean value which indicates if the database is already filled.
You can store the data using this code:
SharedPreferences pref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(key, value);
editor.commit();
and then when you start the application you just read your value using:
SharedPreferences pref = getPreferences(MODE_PRIVATE);
pref.getBoolean(value, false);
// Check if DB is set up on start
SharedPreferences settings = getSharedPreferences("yourappidentifiername",MODE_PRIVATE);
String DbSetUp = settings.getString("dbsetup", "no");
// DbSetUp is "no" or "yes"
// Set that DB is set up
SharedPreferences settings = getSharedPreferences("yourappidentifiername",MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("dbsetup", "yes");
editor.commit();
Not an optimal solution, but you could check for one of your entries, if they don't exist, add them to your DB, otherwise ignore them.
Try this....
Have one bool value in shared preferences.
Check that variable is set or not on each start of the application.
if not set then set it and perform db operation.
if set don't do db operation just go to next steps.
I've got an idea, reading other answers.
Use a version for the database to write in the sharedpreference, so next time you'll need to update the database, just delete the old one and refill the new one with you data.
public void populateResDB(int version){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int resDbVersion = prefs.getInt("ResDBVersion", 1);
if(resDbVersion<version){
//Populate database here
//Delete table/db
//Populate the table/database with new or updated data
//Update ResDBVersion for the next time
Editor editor = prefs.edit();
editor.putInt("ResDBVersion", version);
editor.commit();
}else{
//installed db has the same version as the one we want to install
//so, do nothing.
}
}
I'm trying it now, not yet tested.
In the onCreate of the splash activity use:
populateResDB(1); //1 is for the first time, increase it manually
You can use method onCreate in your database helper - it's used only, when new database is created, so I think that is what you're looking for.