saving the values in database of sd card in android - android

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

Related

Android save EditText text

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", "");

save strings to string.xml, Android

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", "");

How to save a couple of strings to internal storage in android?

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

using shared preferences in android to fill data in forms

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

Best way to store a single user in an Android app?

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.

Categories

Resources