2012-10-22 72 views
0

我在我的窗口小部件配置类有联系人姓名2串和我保存它用下面的代码:如何保存在SharedPreferences

String name= mAppWidgetPrefix.getText().toString(); 
save(context, mAppWidgetId, name); 

static void save(Context context, int appWidgetId, String text) { 
SharedPreferences.Editor prefs = context.getSharedPreferences(
PREFS_NAME, 0).edit(); 
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text); 
prefs.commit(); 
} 

我这个加载:

static String load(Context context, int appWidgetId) { 
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); 
String prefix = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null); 

return prefix; 

} 

它工作正常,但我也想保存电话号码。我如何需要更改代码?

String numberPrefix = pickedNumber.getText().toString(); //I need to save this too 

回答

2

只需指定另一个保存键即可。像这样:

SharedPreferences.Editor prefs = context.getSharedPreferences(
PREFS_NAME, 0).edit(); 
prefs.putString(PREF_PREFIX_KEY_NUMBER + appWidgetId, text); 
prefs.commit(); 
+0

你的回答很有帮助。我有另一个问题,但我没有找到解决方案。请检查它是否有时间:http://stackoverflow.com/questions/13025290/displaying-contacts-photo-by-photo-id – keybee