I'm trying to create a way to get input from a user and save the string in the string.xml, so that when I launch my Application again, it will be there. Can I use the string.xml or do I have to save it another way?
Can I use the string.xml or do I have to save it another way?
You will have to do something different. The resources can't be changed after it has been compiled.
Have a look at Storage Options to see which way is best for you. The best way will be determined by things such as the type and amount of data you will be storing.
The link I posted sums it up pretty well then you can dig into the structures that you think might work best for your situation. From the link:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
in order to save your strings you can use SharedPreferences
to save your data
SharedPreferences pos;
public String fileName = "file";
pos = getSharedPreferences(fileName, 0);
SharedPreferences.Editor editor = pos.edit();
editor.putString("pwd","your string");
editor.commit();
to get your data
pos = getSharedPreferences(fileName, 0);
String data = pos.getString("pwd", "");
Related
I am using the Preference-API..
Typically when I need to retrieve the value of a Preference, I currently do something like this:
final SharedPreferences getPrefs =
PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean isThisPrefEnabled = getPrefs.getBoolean(REFERENCE_TO_PREF_NAME, false);
// OR
String theChosenPref = getPrefs.getString(PREF_NAME, DEFAULT_VALUE);
But I'm curious, couldn't I also do it like this? and if so, what is the difference?
Preference nameOfPref = findPreference(PREFERENCE_KEY);
boolean isPrefEnabled = nameOfPref.isEnabled();
// OR
String thePrefValue = nameOfPref.toString();
It seems to be more efficient, but the first example seems to be what get's used. Why is this?
Thanks.
SharedPreferences is an android specific interface documentation
android.content.SharedPreferences : is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.
Preferences is a core java class documentation
java.util.prefs.Preferences : This class allows applications to store
and retrieve user and system preference and configuration data. This
data is stored persistently in an implementation-dependent backing
store.
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
So I'm working on a project like this http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ which let's me to display my database's values to my android application GUI.
I need to save the database values I need to the android internal storage so I can access it even if my application is not connected to the server. Any help?
Thank you
You can write whole json as string in shared prefernces, and then get it and parse it to display in GUI even when device is offline:
String str = json.toString();
SharedPreferences sharedPref = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE );
SharedPreferences.Editor prefEditor = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE ).edit();
prefEditor.putString( "json", str );
prefEditor.commit();
If you chose local sqlite database as your solution, I wrote a lightweight utility for saving your json into a sqlite.
checkout git repo here. https://github.com/wenchaojiang/JSQL
You only need one line of code.
new JSQLite(yourJsonString, yourDB).persist();
Hope it helps
Consider using a local database to cache the server data locally, that is how most apps does it, here is a good tutorial for sqlite on android Android SQLite
If you use only one or few JSONObjects from the server you can use SharedPreferences, it is much easier and faster to edit/update. example
For more about android storage: Android Storage
I'm working on an application that connects to a php-mysql database in our server, instead of saving the result of the query in a file, you should save it (at least that is what I think so) in an internal Android Database (sqlite). There is a lot of information about databases in Android.
With this example you can see how to easily use sqlite and ContentProviders (a cleaner way of accessing data saved in your database.
In order to save correcty an JSONArray in your database i recommend you to use Jackson libraries in order to create objects from JSON making them easier to be saved.
Finally if the amount of information is relatively small you can use SharedPreferences aswell, this way the data can be accessed faster because it's saved in the mobile memory.
Hope it helps :)
//if you have already the json string
String str = json.toString();
//if you want to convert list to json (with Gson):
//foo - will be your list
String str = new Gson().toJson(foo );
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("pref_json_key", str);
prefEditor.apply();
You can try to use with Realm.io A Simple Example like this:
public class City extends RealmObject {
private String city;
private int id;
// getters and setters left out ...
}
// Insert from a string
realm.beginTransaction();
realm.createObjectFromJson(City.class, "{ city: \"Copenhagen\", id: 1 }");
realm.commitTransaction();
Regards!!!
Okay, I have come across many tutorials on creating and writing/reading a file. What I want my Android app is to create a small text file where it will read and write some settings so my user information can be saved. The problem is every tutorial I have come across uses the SDcard (which allows the user and any other app to read) or use MODE_WORLD_READABLE which makes any app read off of it.
I was wondering how I can create and read/write my file but private or atleast keep it on the internal storage of the phone.
Here's an easy example. To read a preference do this:
SharedPreferences preferences = getSharedPreferences("YourAppName", MODE_PRIVATE);
String thisString = preferences.getString("KeyForThisString", "default");
You can getInt, getLong, etc.
To store the preferences, use these lines:
SharedPreferences preferences = getSharedPreferences("YourAppName",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("KeyForThisString", thisString);
editor.commit();
As before, you can putInt, putLong, etc.
The available methods for this API are here: http://developer.android.com/reference/android/content/SharedPreferences.html
dont know how to use it much and from what im seeing at least, its not much of a difference unless im wrong.
I still recommend to use SharedPreferences technique. It's explicitly easy and save a lot of time.
You can use it to save any primitive data: booleans, floats, ints, longs, and strings.
For example, you can do that each time when user invokes any changes to your Application because it's high performance technique. By this way even on crash all edited info will be stored.
Suppose you have an Activity.
Use this method where data is your String that you wanted to save to file.
private void saveInfo(String data){
String key = "myKey";
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(key , data);
editor.commit();
}
Now, when you launch your application again onCreate method invoked and you can load your information back:
private void loadInfo(){
String key = "myKey";
SharedPreferences mPrefs = context.getSharedPreferences(LauncherUI.PREFS_NAME, 0);
String yourData = mPrefs.getString(key, ""); // "" means if no data found, replace it with empty string
}
Here is a link to 5 types to store data: Storage Options
I am developing a small android application in which I have to save the values in database.
How should I pursue?
Do you want to use database or shared preferences for your applications? If you are looking for shared preferences, the following is a piece of code that demonstrates how to store and retrieve a username and password.
//Saving the username and password
editor = getSharedPreferences("SampleApps", 0).edit();
editor.putString("userName", "David");
editor.putString("password", "Bravo");
editor.commit();
//Retrieving username and password
SharedPreferences sharedPreferences = getSharedPreferences("SampleApps", 1);
String userName=sharedPreferences.getString("userName", null);
String password=sharedPreferences.getString("password", null);
I hope this is what you are looking for.
In Android, you can use both Shared preferences and database to save the data permanently in the device.
By using Shared preferences, the data are saved in XML format. You can find the data in /data/data/yourPackage/shared_prefs. It is a simple format recommended to have variables.
For more complicated structure, you should use the database that comes with Android, which is SQlite. You can find the database in /data/data/yourPackage/databases SQL sentences could be used to create tables and add values. For easier use, an adapter is recommended.
For more information, there is very good tutorial in the Android official page:
http://developer.android.com/guide/topics/data/data-storage.html