{"id":704,"date":"2023-03-02T16:54:11","date_gmt":"2023-03-02T07:54:11","guid":{"rendered":"https:\/\/www.dogrow.net\/android\/?p=704"},"modified":"2023-03-03T09:56:46","modified_gmt":"2023-03-03T00:56:46","slug":"blog36","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/android\/blog36\/","title":{"rendered":"(36) SharedPreferences\u3067Key-Value\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u66f8\u304d\u3059\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u4ee5\u4e0b\u306e Android\u516c\u5f0f\u30da\u30fc\u30b8\u306e\u8a18\u8ff0\u306b\u5f93\u3063\u3066 <span class=\"my_fc_deeppinkB\">SharedPreferences<\/span> \u3092\u4f7f\u3044 <span class=\"my_fc_deeppinkB\">Key-Value\u578b<\/span> \u306e\u30c7\u30fc\u30bf\u306e Read\/Write\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f\u3002<br \/>\n<a href=\"https:\/\/developer.android.com\/training\/data-storage\/shared-preferences?hl=ja\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.android.com\/training\/data-storage\/shared-preferences<\/a><\/p>\n<h1 class=\"my_h\">2. \u3084\u3063\u3066\u307f\u308b<\/h1>\n<h2 class=\"my_h\">1) \u4f5c\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4ed5\u69d8<\/h2>\n<p>1. \u753b\u9762\u4e0a\u306b\u3001EditText, Button(WRITE), Button(READ), TextView \u306e\u5168 4\u500b\u306e View\u3092\u914d\u7f6e\u3059\u308b\u3002<br \/>\n2. EditText\u306b\u30c6\u30ad\u30b9\u30c8\u3092\u5165\u529b\u3057\u3001Button(W)\u3092\u62bc\u4e0b\u3059\u308b\u3068\u3001SharedPreferences\u306b\u30c6\u30ad\u30b9\u30c8\u3092\u4fdd\u5b58\u3059\u308b\u3002<br \/>\n3. Button(R)\u3092\u62bc\u4e0b\u3059\u308b\u3068\u3001SharedPreferences\u306b\u4fdd\u5b58\u3055\u308c\u305f\u5168\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u51fa\u3057\u3001TextView\u306b\u4fdd\u5b58\u3059\u308b\u3002<br \/>\n4. SharedPreferences\u306b\u4fdd\u5b58\u3059\u308b\u30c7\u30fc\u30bf\u306e <span class=\"my_fc_deeppinkB\">Key-Value<\/span> \u306e\u7d44\u307f\u5408\u308f\u305b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<br \/>\n<table class=\"my_tbl_simple\">\n<tr><td>#<\/td><td><span class=\"my_fc_deeppinkB\">key<\/span><\/td><td><span class=\"my_fc_deeppinkB\">value<\/span><\/td><\/tr><tr><td>1<\/td><td><b>n<\/b><\/td><td>\u767b\u9332\u6570<\/td><\/tr><tr><td>2<\/td><td><b>t1<\/b><\/td><td>\u30c6\u30ad\u30b9\u30c8(1\u756a\u76ee)<\/td><\/tr><tr><td>&nbsp;<\/td><td><b>t2<\/b><\/td><td>\u30c6\u30ad\u30b9\u30c8(2\u756a\u76ee)<\/td><\/tr><tr><td>&nbsp;<\/td><td>:<\/td><td>:<\/td><\/tr><tr><td>&nbsp;<\/td><td><b>tn<\/b><\/td><td>\u30c6\u30ad\u30b9\u30c8(n\u756a\u76ee)<\/td><\/tr>\n<\/table><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/android\/wp-content\/uploads\/2023\/03\/i001.jpg\" alt=\"\"  \/><\/p>\n<h3 class=\"my_h\">(1) \u30ea\u30bd\u30fc\u30b9<\/h3>\n<p>\u5171\u6709\u74b0\u5883\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306e\u30e6\u30cb\u30fc\u30af\u306a\u8b58\u5225\u5b50\u3092\u4f5c\u308b\u3002<br \/>\n<span class=\"my_fc_deeppinkB\">{applicationId}.PREFERENCE_FILE_KEY<\/span><br \/>\n\u306b\u3057\u3066\u304a\u3051\u3070\u3001\u4ed6\u306e\u30a2\u30d7\u30ea\u3068\u306f\u88ab\u3089\u306a\u3044\u306f\u305a\u3001\u3068\u306e\u3053\u3068\u3002(\u4e0a\u8a18\u306e Android\u516c\u5f0f\u30b5\u30a4\u30c8\u306e\u8aac\u660e\u3092\u53c2\u7167\u306e\u3053\u3068)<\/p>\n<pre class=\"brush: xml; highlight: [3]; title: ; notranslate\" title=\"\">\r\n&lt;resources&gt;\r\n    &lt;string name=&quot;app_name&quot;&gt;TestSharedPreferences&lt;\/string&gt;\r\n    &lt;string name=&quot;preference_file_key&quot;&gt;com.example.testsharedpreferences.PREFERENCE_FILE_KEY&lt;\/string&gt;\r\n&lt;\/resources&gt;\r\n<\/pre>\n<h3 class=\"my_h\">(2) Java\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.example.testsharedpreferences;\r\n\r\nimport androidx.appcompat.app.AppCompatActivity;\r\n\r\nimport android.content.Context;\r\nimport android.content.SharedPreferences;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.EditText;\r\nimport android.widget.TextView;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n    private SharedPreferences sharedPref;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n\r\n        this.sharedPref = this.getSharedPreferences(\r\n                getString(R.string.preference_file_key), Context.MODE_PRIVATE);\r\n    }\r\n\r\n    \/* \u4fdd\u5b58\u3059\u308b\u30c7\u30fc\u30bf\u69cb\u9020\r\n        n : \u30c7\u30fc\u30bf\u6570\r\n        t1\uff5etn : \u30c7\u30fc\u30bf(Text)\r\n     *\/\r\n    private void ExecSave(String txt){\r\n        int n = this.sharedPref.getInt(&quot;n&quot;, 0);\r\n        if(txt != &quot;&quot;){\r\n            String newKey = String.format(&quot;t%d&quot;, n);\r\n            n++;\r\n\r\n            SharedPreferences.Editor editor = this.sharedPref.edit();\r\n            editor.putInt(&quot;n&quot;, n);\r\n            editor.putString(newKey, txt);\r\n            editor.apply();\r\n        }\r\n    }\r\n\r\n    private List&lt;String&gt; ExecRead(){\r\n        List&lt;String&gt; list = new ArrayList&lt;&gt;();\r\n        int n = this.sharedPref.getInt(&quot;n&quot;, 0);\r\n        for(int i = 0 ; i &lt; n ; i++ ){\r\n            String key = String.format(&quot;t%d&quot;, i);\r\n            String val = this.sharedPref.getString(key, &quot;*&quot;);\r\n            list.add(val);\r\n        }\r\n        return list;\r\n    }\r\n\r\n    public void onPushWrite(View view){\r\n        EditText et = this.findViewById(R.id.et_text);\r\n        String txt = et.getText().toString();\r\n        ExecSave(txt);\r\n    }\r\n\r\n    public void onPushRead(View view){\r\n        List&lt;String&gt; txtList = this.ExecRead();\r\n        String txt = &quot;&quot;;\r\n        for(String s : txtList){\r\n            txt += s + &quot;\\n&quot;;\r\n        }\r\n        TextView tv = this.findViewById(R.id.tv_list);\r\n        tv.setText(txt);\r\n    }\r\n\r\n}\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 \u4ee5\u4e0b\u306e Android\u516c\u5f0f\u30da\u30fc\u30b8\u306e\u8a18\u8ff0\u306b\u5f93\u3063\u3066 SharedPreferences \u3092\u4f7f\u3044 Key-Value\u578b \u306e\u30c7\u30fc\u30bf\u306e Read\/Write\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f\u3002 https:\/\/developer.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/android\/blog36\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-704","post","type-post","status-publish","format-standard","hentry","category-androidstudio"],"views":701,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/comments?post=704"}],"version-history":[{"count":17,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/704\/revisions"}],"predecessor-version":[{"id":722,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/704\/revisions\/722"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/media?parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/categories?post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/tags?post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}