开发者

shared preferences in android

开发者 https://www.devze.com 2023-02-06 05:43 出处:网络
how to strore d开发者_如何学Pythonata in shared preferences in table format For store the values using shared preference use below code

how to strore d开发者_如何学Pythonata in shared preferences in table format


For store the values using shared preference use below code

SharedPreferences prefs=getSharedPreferences("Key", 0);
          Editor e=  prefs.edit();
           e.putString("Name", "AAA");
           e.commit();

For retrieve the shared prefs value use below code

SharedPreferences prefs=getSharedPreferences("Key", 0);
          String s= prefs.getString("Name", "");


I don't think you can save table in sharedpreferences; it is saved as like key-value pair.


You can serialize your object and save it in the preferences. Use Serializable or JSON or Protocol Buffers or whatever you're comfortable with.


SharedPreferences store primitive data in a file as key-value pairs.

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings.

http://developer.android.com/guide/topics/data/data-storage.html#pref

I adviese to use SQLITE Database to store data in table format.

http://developer.android.com/guide/topics/data/data-storage.html#db


Shared preferences = "Key" , "Value" sets.

If we wants to store small amount of data like usernames, passwords then we will go for Shared Preferences.

To store small amount of data there is no need of creation of Database, tables, insertion query and retrieval query --- So better to go for Shared Preferences.

Shared Preferences is to stores small amount of data, where as SQLite3 is to store large amount of data.

Shared preferences works like HashMap in Collections.

Shared preferences data will be stored in xml file. This xml file we can find in the following location.

Go to

  1. Open DDMS perspective.
  2. Select emulator / rooted Device from the left side panel.
  3. Select File explorer tab from the right side panel.
  4. Open data folder.
  5. Again open data folder.
  6. Open our package name.
  7. Here you will find the Shared Preference folder.
  8. Inside this Shared Preference folder - our shared preference xml file will be visible.
  9. We can pull this xml file from the emulator by selecting the xml file and click on Left arrow on top right side of the DDMS window.
  10. We can also change values in the xml file, then we can push this changed xml file into emulator by clicking on the Right arrow which is there on top right side of this DDMS window.

Note: When you push anything into emulator "DONT FORGET TO RESTART YOUR EMULATOR". Otherwise the changes will not be effected.

Storing the values into shared preferences

import android.content.SharedPreferences;
SharedPreferences preference;
SharedPreferences.Editor editor;
preference=getApplicationContext().getSharedPreferences("PROFILE", 0);
editor=preference.edit();

editor.putString("MANUALPROFILENAME", newProfileValue);

editor.commit();

For getting the values from the shared preferences

import android.content.SharedPreferences;
SharedPreferences preference;
SharedPreferences.Editor editor;
preference=getBaseContext().getSharedPreferences("PROFILE", 0);
String manualsetunset =preference.getString("MANUALPROFILEENAME", "false");// Here false is default value. If the required string does not found in shared preference, the default value will be stored in the string object.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号