Site icon C1CTech

Save key-value data(SharedPreferences)

<p>There are many ways of storing data in android&comma; One of the simplest way is SharedPrefrences If you have a relatively small collection of key-values to save&comma; you should use the<span style&equals;"font-family&colon; monospace&semi;"> SharedPrefrences<&sol;span> APIs&comma; Using SharedPrefrences You can <span style&equals;"color&colon; &num;000000&semi;">stores<&sol;span>&colon;   <span style&equals;"color&colon; &num;008000&semi;"><strong>String&comma; Integer&comma; Float&comma; Long&comma; Double and a String Set&period; <&sol;strong><&sol;span><&sol;p>&NewLine;<p>download the full project from  <strong><a href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;SharedPreference">Github&period;<&sol;a><&sol;strong><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;">Save data to shared preferences<&sol;span><&sol;h4>&NewLine;<pre><code>SharedPreferences sharedPreferences&equals;getSharedPreferences&lpar;"AppName"&comma;MODE&lowbar;PRIVATE&rpar;&semi;&NewLine;SharedPreferences&period;Editor editor&equals;sharedPreferences&period;edit&lpar;&rpar;&semi;&NewLine;&NewLine;editor&period;putString&lpar;"String"&comma;"string"&rpar;&semi;&NewLine;editor&period;putBoolean&lpar;"bool"&comma;true&rpar;&semi;&NewLine;editor&period;putFloat&lpar;"float"&comma;1&period;0f&rpar;&semi;&NewLine;editor&period;putInt&lpar;"int"&comma;1&rpar;&semi;&NewLine;editor&period;putLong&lpar;"long"&comma;1&rpar;&semi;&NewLine;&NewLine;editor&period;apply&lpar;&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Parameter&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;appName&&num;8221&semi;<&sol;strong><&sol;span><br &sol;>&NewLine;is name of the SharedPreference which will be useful when you read data from shared<br &sol;>&NewLine;preferences&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;MODE&lowbar;PRIVATE&&num;8221&semi;<&sol;strong><&sol;span><br &sol;>&NewLine;By setting this mode&comma; the file can only be accessed using by only your application&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;Stringkey&&num;8221&semi;<&sol;strong><&sol;span><br &sol;>&NewLine;is the Key for save and retrieve the value&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>apply&lpar;&rpar;<&sol;strong><&sol;span> or <span style&equals;"color&colon; &num;008000&semi;"><strong>commit&lpar;&rpar;<&sol;strong><&sol;span> to save the changes after putting data into the editor&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Difference between apply&lpar;&rpar; and commit&lpar;&rpar;&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>apply&lpar;&rpar; write the data to disk asynchronously and commit write the data<br &sol;>&NewLine;synchronously We should avoid calling the commit&lpar;&rpar; from main thread because<br &sol;>&NewLine;it could pause UI rendering&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Read from shared preferences<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre><code>SharedPreferences sharedPref &equals; getSharedPreferences&lpar;"AppName"&comma;MODE&lowbar;PRIVATE&rpar;&semi;&NewLine;String stringValue&equals;sharedPref&period;getString&lpar;"string"&comma;""&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Parameters&colon;<br &sol;>&NewLine;<&sol;strong><&sol;span><span style&equals;"color&colon; &num;008000&semi;">StringKey<&sol;span> is the key for getting the value&period;<br &sol;>&NewLine;optionally a default value to return if the key isn&&num;8217&semi;t present&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;">Full code&colon;<&sol;span><&sol;h4>&NewLine;<pre><code>package c1c&period;sharedpreferenceexp&semi;&NewLine;import android&period;content&period;SharedPreferences&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;import java&period;util&period;HashSet&semi;&NewLine;import java&period;util&period;Set&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; &sol;&sol;Save the data in shared Preference&period;&NewLine; SharedPreferences sharedPreferences&equals;getSharedPreferences&lpar;"AppName"&comma;MODE&lowbar;PRIVATE&rpar;&semi;&NewLine; SharedPreferences&period;Editor editor&equals;sharedPreferences&period;edit&lpar;&rpar;&semi;&NewLine;&NewLine; editor&period;putBoolean&lpar;"bool"&comma;true&rpar;&semi;&NewLine; editor&period;putFloat&lpar;"float"&comma;1&period;0f&rpar;&semi;&NewLine; editor&period;putInt&lpar;"int"&comma;1&rpar;&semi;&NewLine; editor&period;putLong&lpar;"long"&comma;1&rpar;&semi;&NewLine; editor&period;putString&lpar;"String"&comma;"string"&rpar;&semi;&NewLine;&NewLine; Set&lt&semi;String&gt&semi; set&equals;new HashSet&lt&semi;String&gt&semi;&lpar;&rpar;&semi;&NewLine; set&period;add&lpar;"element1"&rpar;&semi;&NewLine; set&period;add&lpar;"element2"&rpar;&semi;&NewLine; set&period;add&lpar;"element3"&rpar;&semi;&NewLine; set&period;add&lpar;"element4"&rpar;&semi;&NewLine; editor&period;putStringSet&lpar;"stringSet"&comma;set&rpar;&semi;&NewLine; editor&period;apply&lpar;&rpar;&semi;&NewLine;&NewLine; &sol;&sol;get the data from shared Prefernces&period;&NewLine; SharedPreferences sharedPref &equals; getSharedPreferences&lpar;"AppName"&comma;MODE&lowbar;PRIVATE&rpar;&semi;&NewLine;&NewLine; boolean bool&equals;sharedPref&period;getBoolean&lpar;"bool"&comma;false&rpar;&semi;&NewLine; Log&period;e&lpar;"bool "&comma;""&plus;bool&rpar;&semi;&NewLine;&NewLine; float floatValue&equals;sharedPref&period;getFloat&lpar;"float"&comma;0&rpar;&semi;&NewLine; Log&period;e&lpar;"floatValue "&comma;""&plus;floatValue&rpar;&semi;&NewLine;&NewLine; int intValue&equals;sharedPref&period;getInt&lpar;"int"&comma;0&rpar;&semi;&NewLine; Log&period;e&lpar;"int "&comma;""&plus;intValue&rpar;&semi;&NewLine;&NewLine; long longValue&equals;sharedPref&period;getLong&lpar;"long"&comma;0&rpar;&semi;&NewLine; Log&period;e&lpar;"long "&comma;""&plus;longValue&rpar;&semi;&NewLine;&NewLine; String stringValue&equals;sharedPref&period;getString&lpar;"string"&comma;""&rpar;&semi;&NewLine; Log&period;e&lpar;"stringValue "&comma;""&plus;stringValue&rpar;&semi;&NewLine;&NewLine; Set&lt&semi;String&gt&semi; setvalue&equals;sharedPref&period;getStringSet&lpar;"stringSet"&comma;null&rpar;&semi;&NewLine; Log&period;e&lpar;"setValue "&comma;""&plus;setvalue&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;

Exit mobile version