Android通过反射打造可以存储任何对象的万能SharedPreferences

我们通常使用SharedPreferences存储一些需要保存在本地,但又不至于存储在数据库里的一些数据,一般我们用它来存储一些用户名,密码等数据是非常方便的,那么如果我们想要存储的信息有10几条,我们就得写10几行重复的代码把他们存进去,取出来的时候还得写10几行代码将他们一条一条取出来,那么有没有什么办法可以只需要一行代码就可以将他们存进去,一行代码就将他们取出来?答案是有的,这篇文章我们就介绍如何通过反射来实现。

1.将我们想要保存的数据封装成一个实体类 Person.java 其中属性我们都用Public来修饰

<a target=_blank id="L1" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
           
           
public class Person {
//String类型属性 姓名
public String name ;
//int类型属性 年龄
public int age ;
//float类型属性 身高
public float height ;
//boolean类型属性 是否已婚
public boolean isMarried ;
//String类型属性 证件号
public String id ;
}
 来自CODE的代码片
Person.java

2.通过反射,将Person类中的属性取出来
<a target=_blank id="L1" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
            
            
public class SharedPreferencesUtils {
/**
* 根据传入的对象,取出其中用public修饰符修饰的属性
* @param clazz 想要拿到属性的类的字节码文件
* @return 所有用pulic修饰的属性的一个list表
*/
public static < T > List < Field > getPublicFields ( Class <?> clazz ){
if ( clazz . equals ( Object . class )) {
return null ;
}
//用来存储clazz中用public修饰的属性的list
List < Field > list = new ArrayList < Field >();
//获得clazz中所有用public修饰的属性
Field [] fields = clazz . getFields ();
//将fields加入到list中
for ( int i = 0 ; i < fields . length ; i ++){
list . add ( fields [ i ]);
}
return list ;
}
 来自CODE的代码片
SharedPreferencesUtils.java
我们先来测试一下属性有没有被取出来,我们在MainActivity.java中添加如下代码进行测试
<a target=_blank id="L1" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
             
             
public class MainActivity extends Activity {
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
//调用我们刚刚写好的方法来获取Person类中的属性
List < Field > publicFields = SharedPreferencesUtils . getPublicFields ( Person . class );
for ( Field f : publicFields ){
String name = f . getName ();
Log . i ( "TAG" , name + "\n" );
}
}
}
 来自CODE的代码片
MainActivity.java
我们来看一下Log打印的信息
我们可以看到,Person中的属性我们都已经拿到了

3.将对象存储到SharedPreferences中
<a target=_blank id="L1" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
              
              
public static void putObjectToShare ( String shareName , Object obj ){
//获得SharedPreferences实例
SharedPreferences sharedPreferences = mContext . getSharedPreferences ( shareName , Activity . MODE_PRIVATE );
//获得Editor
Editor edit = sharedPreferences . edit ();
//存储数据之前先将之前的旧数据清掉
edit . clear ();
//调用commit提交数据(这里为了清掉数据)
edit . commit ();
List < Field > publicFields = getPublicFields ( obj . getClass ());
for ( Field f : publicFields ){
String name = f . getName ();
try {
//获得当前属性的类型和值
//类型的话如果是基本类型,会自动装箱
Object type = f . get ( obj );
//判断各种类型,调用各种类型的put方法将数据存储进去
if ( type instanceof String ) {
edit . putString ( name , ( String ) type );
} else if ( type instanceof Integer ) {
edit . putInt ( name , ( Integer ) type );
} else if ( type instanceof Float ) {
edit . putFloat ( name , ( Float ) type );
} else if ( type instanceof Long ) {
edit . putLong ( name , ( Long ) type );
} else if ( type instanceof Boolean ) {
edit . putBoolean ( name , ( Boolean ) type );
}
} catch ( Exception e ) {
e . printStackTrace ();
}
//调用commit,提交数据
edit . commit ();
}
}
 来自CODE的代码片
SharedPreferencesUtils.java

4.将对象从SharedPreferences中取出来
<a target=_blank id="L1" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank id="L37" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank id="L38" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank id="L39" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
               
               
public static < T > T getObjectFromShare ( String shareName , Class < T > clazz ){
//获得SharedPreferences实例
SharedPreferences sharedPreferences = mContext . getSharedPreferences ( shareName , Activity . MODE_PRIVATE );
//T是一个泛型,根据clazz不同而不同
T t = null ;
try {
//获得sharedPreferences中所有的数据,数据为键值对保存在map中
Map < String ,?> map = sharedPreferences . getAll ();
//调用getPublicFields方法得到clazz中所有的公有属性
List < Field > publicFields = getPublicFields ( clazz );
//如果两者都不为空的话
if ( map . size ()> 0 && publicFields . size ()> 0 ) {
//将T实例化出来
t = clazz . newInstance ();
//遍历map中所有的键值对
for ( Map . Entry < String ,?> entry : map . entrySet ()){
//map中的键
String key = entry . getKey ();
//map中的值
Object value = entry . getValue ();
//遍历clazz中的所有公有属性
for ( Field field : publicFields ){
//获得属性名
String name = field . getName ();
//如果属性名与键相同
if ( name . equalsIgnoreCase ( key )) {
//相当于给对象T中的属性field赋值,值为value
field . set ( t , value );
break ;
}
}
}
}
} catch ( Exception e ) {
e . printStackTrace ();
}
//整个遍历结束后,我们的对象中的属性也都有值了
return t ;
}
 来自CODE的代码片
SharedPreferencesUtils.java

5.我们来测试一下,修改MainAcitvity.java中的代码
<a target=_blank id="L1" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/nugongahou110/article/details/46913461#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
                
                
public class MainActivity extends Activity {
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
//实例化一个Person实例,这是我们想要存储到SharedPreferences中的对象
Person p = new Person ( "zhangsan" , 23 , 175.0f , false , "001" );
//将p存储到SharedPreferences的“zhangqi”名下
SharedPreferencesUtils . getInstance ( this ). putObjectToShare ( "zhangqi" , p );;
//从SharedPreferences的“zhangqi”名下取出之前存储过的Person实例
Person savedPerson = SharedPreferencesUtils . getInstance ( this ). getObjectFromShare ( "zhangqi" , Person . class );
Log . e ( "zhangqi" , "" + savedPerson . toString ());
}
}
 来自CODE的代码片
MainActivity.java
我们看一下Log中打印的savedPerson的信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值