shared preferences in android

后端 未结 5 798
陌清茗
陌清茗 2020-12-11 13:54

how to strore data in shared preferences in table format

5条回答
  •  抹茶落季
    2020-12-11 14:27

    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.
    

提交回复
热议问题