开发者

How to set more than one shared preference, without first creating xml

I am trying to setup more than one shared preference with this method. This will successfully create one shared pref:

 static final String SUPPLIER_NUMBER = "";

   SharedPreferences myPrefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
   savednumber = myPrefs.getString(SUPPLIER_NUMBER, "");

           SharedPreferences myPrefs1 = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
           SharedPreferences.Editor prefsEditor = myPrefs1.edit();
           prefsEditor.putString(SUPPLIER_NUMBER, telephonenumber);
           prefsEditor.commit();

This second example simply results in the second pref overriding the first....??? What am I missing here?

 static final String SUPPLIER_NUMBER = "";
 static final String SUPPLIER_COST = "";

       SharedPreferences myPrefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
       savednumber = myPrefs.getString(SUPPLIER_NUMBER, "");
       savedcost = myPrefs.getString(SUPPLIER_COST, "");

               SharedPreferences myPrefs1 = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
               SharedPreferences.Editor prefsEditor = myPrefs1.edit();
               prefsEditor.putString(SUPPLIER_NUMBER, telephonenumber);
               prefsEditor.putString(SUPPLIER_COST, suppliercost);
               prefsEditor.commit();

I do not really want to create an xml file to get the prefs from.. I want it dynamically created, as I believe I am getting here, in the first example.. but I need to be able to add more th开发者_开发百科an one preference.


Well your SUPPLIER_NUMBER and SUPPLIER_COST constants are equal (both are ""). Set them to different values and that should do the trick :)

 static final String SUPPLIER_NUMBER = "number";
 static final String SUPPLIER_COST = "cost";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜