2014-01-07 63 views
0

我想允許設置不同的首選項,通過PreferenceScreen保存在數據庫中的聯繫人。對於我讀,爲了創建多個SharedPreferences,可以這樣做:android從數據庫的聯繫人列表的不同偏好

SharedPreferences prefContact1 = getSharedPreferences("micheal", Context.MODE_PRIVATE); 
SharedPreferences prefContact2 = getSharedPreferences("john", Context.MODE_PRIVATE); 
SharedPreferences prefContact3 = getSharedPreferences("frady", Context.MODE_PRIVATE); 
// and so on 

所以我創建不同的喜好像上面的位置:

// create contact-based pref 

String phone = cursor.getString(cursor.getColumnIndexOrThrow("phone")); 
user_preference = getSharedPreferences(phone, Context.MODE_PRIVATE); 

// start contact pref screen/activity 
Intent intent = new Intent(this, ContactSettingActivity.class); 
intent.putExtra("phone", phone); 

startActivity(intent); 

而且在ContactSettingActivity,我有這個代碼:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

Intent intent = getIntent(); 
String phone = intent.getStringExtra("phone"); 

this.addPreferencesFromResource(R.xml.user_preferences); 
this.initSummaries(this.getPreferenceScreen()); 

// get contact's own pref 
SharedPreferences prefs = getSharedPreferences(phone, MODE_PRIVATE); 
prefs.registerOnSharedPreferenceChangeListener(this); 

// ... more code 

這裏是我的user_preferences.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 
    <PreferenceCategory android:title="Notification Settings" > 
     <RingtonePreference 
      android:defaultValue="content://settings/system/notification_sound" 
      android:key="user_prefRingtone" 
      android:ringtoneType="notification" 
      android:showDefault="true" 
      android:showSilent="true" 
      android:summary="Default ringtone for incoming SMS" 
      android:title="SMS Ringtone" /> 

     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="user_prefVibrate" 
      android:summary="Vibrate on incoming SMS" 
      android:title="Vibrate" > 
     </CheckBoxPreference> 

     <ListPreference 
      android:defaultValue="0" 
      android:entries="@array/pref_notif_icon_entries" 
      android:entryValues="@array/pref_notif_icon_values" 
      android:key="user_prefnotificon" 
      android:summary="Change the notification area icon" 
      android:title="Notification Icon" /> 
     <ListPreference 
      android:defaultValue="Off" 
      android:entries="@array/pref_led_color_entries" 
      android:entryValues="@array/pref_led_color_values" 
      android:key="user_prefled" 
      android:summary="Color of the LED" 
      android:title="LED Color" /> 

     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="user_prefWakeup" 
      android:summary="Wakeup screen on incoming SMS" 
      android:title="Screen Wakup" > 
     </CheckBoxPreference> 
    </PreferenceCategory> 
</PreferenceScreen> 

現在的問題是,即使我通過getSharedPreferences(phone, Context.MODE_PRIVATE)設置基於聯繫人的電話不同的喜好,但仍當我去到任何用戶的喜好屏幕,它始終是適用於所有聯繫人:(

可以在相同的設置任何人都會提示我做錯了什麼,或者如何爲ListView的項目(聯繫人)設置不同的首選項,以便每個人都有不同的通知聲音,通知圖標等?或者說有用的其他任何方法也歡迎:)

感謝您的幫助:)

回答

0

好,我找到了,我加下面一行的設置活動:

this.getPreferenceManager().setSharedPreferencesName(phone); 

這裏更新的代碼:

public class ContactSettingActivity extends PreferenceActivity implements 
     OnSharedPreferenceChangeListener { 

    @SuppressWarnings("deprecation") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Intent intent = getIntent(); 
     String phone = intent.getStringExtra("phone"); 

     SharedPreferences prefs = getSharedPreferences(phone, MODE_PRIVATE); 
     this.getPreferenceManager().setSharedPreferencesName(phone); 
     this.addPreferencesFromResource(R.xml.user_preferences); 
     this.initSummaries(this.getPreferenceScreen()); 
     prefs.registerOnSharedPreferenceChangeListener(this); 
    }