Android共享参数SharedPreferences

来源:互联网 发布:fifa online 3软件 编辑:程序博客网 时间:2024/05/22 07:51

一、共享参数SharedPreferences保存

       SharedPreferences shared=context.getSharedPreferences(name, mode)

       context:上下文对象  

       参数:name  创建的xml文件名

               mode:  int类型  常用值为0 

       在实例化 Editor

       Editor editor=shared.edit();

       editor.putString(String name,String value);//键值对  保存相应的数据.

       editor.commit();//最后一定要提交一次 ,否则数据没有保存

        注:若要每次清理保存的数据调用 editor.clear().commit();


二、共享参数SharedPreferences提取

          SharedPreferences shared=context.getSharedPreferences(name, mode);//注意文件name必须与上面保存时的文件name一致。

          shared.getString(String arg0,String arg1);arg0是要获取值的键  arg1是若没有这个键,设置一个默认值,一般为null

     

三、课外补充  ArrayList< >  HashSet<>   LinkedHashSet<>

                          注:若在保存数据时,map与共享参数一起使用,数据清理时 editor.clear().commit()。还要把list集合调用.clear();

                         ArrayList<> 允许保存重复的值 ,并按输入的顺序进行排列

                         HashSet<> 不允许保存重复的值,顺序排列没有规律

                         LinkedHashSet<>  不允许保存重复的值,按输入的顺序进行保存

                        重点:对ArrayList<>重复数据进行删除 有两种解决办法

                        

 HashSet<String> set = new HashSet<String>(listWithDuplicateElements);        ArrayList<String> list = new ArrayList<String>(set);
 LinkedHashSet<String> set = new LinkedHashSet<String>(listWithDuplicateElements);        ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);

0 0