2012-11-26 137 views
0

我创建SharedPreference OBJ使用下列内容: 注:在相同的首选项文件名创建喜好的数量android共享偏好存储多个值;

public static Sting name = "abx"; //pref file name 
      spo = context.getsharedpreference(name , mode_private); 
      editor = spo.edit(); //get the editor associated with pref obj 

     Map<String, String> test= new HashMap<String, String>();//create a map 
    test.put("1", "abc");//fill the values 
    test.put("2", "efg");//used map because values are stored in key -value pairs 
    test.put("3", "ghj"); 
     for (String s : test.keySet()) { 
     // use the name as the key, and the icon as the value 
     editor.putString(s, nameIcons.get(s));//use the key as preference name 
    } 

    editor.commit();//commit the preferences 

我有一个疑问,请纠正我,如果我错了,有名字SharedPreference文件“ABZ”是创建并在此文件中的首选项值存储为键值对,或者如果编辑器obj为每个键创建新的首选项文件,在创建共享首选项对象时提供pref文件名的用法是什么?

回答

1

本质上,您可以为给定的应用程序创建多个首选项文件。有几个原因可以设想这么做,如果你有多个账户,最好的就是这样。您可以标记一组偏好“account1”和另一个“account2”。只要跟踪您所在的帐户,您就可以共享代码,而不需要太多的复杂性。

除非您特别使用此功能,否则我建议您使用context.getApplicationContext().getPackageName()作为名称,因为它应该几乎在任何地方都可用。

0

基本上每个循环内部发生了什么: - 只有一个值存储在首选项中,而不是使用代码中为地图中每个值提供的名称创建SharedPreferance文件。

在创建sharedPreferance期间使用给予文件名,以便可以在应用程序的其他部分检索并可以访问其值。