android SharedPreferences 的问题,读不到数据。
android存储SharedPreferences后无法获取到数据 个人在应用android SharePreferences时由于用法错误导致的无法获取到存储数据问题。在此记录,以作提醒 阅读详情
SharedPreferences share = getSharedPreferences("name", 0);
Editor e = share.edit();
//方法a,在第一句输出的时候读不到数据。
String a = "abc_cc";
System.err.println(share.getString(a, "默认结果"));//此句似乎没有执行,没有任何输出,错误...
//但是第二次执行的时候,会正常输出。
e.putString(a, "修改的结果");
e.commit();
System.err.println(share.getString(a, "默认结果"));//输出:修改的结果,正常
//方法b,一切正常
System.err.println(share.getString("abc_cc", "默认结果"));//输出:默认结果,正常
e.putString("abc_cc", "修改的结果");
e.commit();
System.err.println(share.getString("abc_cc", "默认结果"));//输出:修改的结果,正常
//求解,为什么方法a不行?这是getString的源码
String getString(String key, String defValue);
/**
* Retrieve a set of String values from the preferences.
*
* <p>Note that you <em>must not</em> modify the set instance returned
* by this call. The consistency of the stored data is not guaranteed
* if you do, nor is your ability to modify the instance at all.
*
* @param key The name of the preference to retrieve.
* @param defValues Values to return if this preference does not exist.
*
* @return Returns the preference values if they exist, or defValues.
* Throws ClassCastException if there is a preference with this name
* that is not a Set.
*
* @throws ClassCastException
*/
...全文
相关推荐
Android 保存List数据(SharedPreferences)
在Android开发中,数据存储是不可或缺的一部分,尤其是在处理用户数据或者应用状态时。本文将深入探讨如何使用SharedPreferences来保存一个List类型的数据。
Android 清除SharedPreferences 产生的数据(实例代码)
以下是关于Android中SharedPreferences的详细说明以及清除数据的实例代码。1.
Android本地存储SharedPreferences详解
在Android开发中,SharedPreferences是一种轻量级的本地数据存储方式,用于保存应用中的简单配置数据。它将数据以键值对的形式存储在XML文件中,位于设备的 `/data/data/<
Android数据共享 sharedPreferences 的使用方法
Android 数据共享 sharedPreferences 的使用方法Android 中的数据共享是一种常见的需求,特别是在Activity之间或App之间需要共享数据的情况下。
安卓学习者——从SharedPreferences中读取数据的实例
在Android应用开发中,SharedPreferences是Android提供的一种轻量级的数据存储方式,主要用于存储一些简单的配置信息,如用户设置、应用状态等。


