我需要一个包含多个联系人详细信息的首选项屏幕 所以我默认有一个按钮和“添加联系人”按钮来添加其他联系人。 出于某种原因,它一切正常,但是当我再次访问设置时,新的首选项不会显示。
当我添加联系人时,所有首选项都会显示 - 并且可以输入数据。 但当我退出并重新进入prefs时,一切都消失了。只显示联系人1。
public class SettingsActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
public PreferenceScreen root;
@SuppressWarnings("deprecation")
public PreferenceScreen createPreferenceHierarchy() {
PreferenceCategory userDetails = new PreferenceCategory(this);
userDetails.setTitle("User Details");
userDetails.setKey("userdetails");
root.addPreference(userDetails);
EditTextPreference userName = new EditTextPreference(this);
userName.setTitle("User Name");
userName.setSummary("Please enter your full name");
userName.setDialogTitle("Please enter your name");
userName.setKey("userName");
userDetails.addPreference(userName);
EditTextPreference contactCount = new EditTextPreference(this);
contactCount.setTitle("Count");
contactCount.setEnabled(false);
contactCount.setKey("contactcount");
userDetails.addPreference(contactCount);
PreferenceCategory contact1 = new PreferenceCategory(this);
contact1.setTitle("Emergency Contact 1");
contact1.setKey("contact1");
contact1.setSummary("Add emergency contact details");
root.addPreference(contact1);
EditTextPreference contactName1 = new EditTextPreference(this);
contactName1.setTitle("Name");
contactName1.setSummary("Emergency Contact Name");
contactName1.setDialogTitle("Please enter the name");
contactName1.setKey("contactname1");
contact1.addPreference(contactName1);
ListView v = getListView();
Button button = new Button(this);
button.setText("Add Contact");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int count = Integer.parseInt(root.getPreferenceManager()
.getSharedPreferences().getString("count", "1"));
int nextCount = count + 1;
EditTextPreference contactName = new EditTextPreference(
SettingsActivity.this);
contactName.setKey("contactname" + nextCount);
contactName.setTitle("Email ID");
contactName.setSummary("Emergency Contact's Name");
PreferenceCategory preferenceCategory = new PreferenceCategory(
SettingsActivity.this);
preferenceCategory.setKey("contact" + nextCount);
preferenceCategory.setTitle("Emergency Contact" + nextCount);
preferenceCategory.setSummary("Add emergency contact details");
root.addPreference(preferenceCategory);
root.addPreference(contactName);
Editor editor = root.getPreferenceManager()
.getSharedPreferences().edit();
editor.putString("count", Integer.toString(++count));
editor.commit();
}
});
v.addFooterView(button);
return root;
}
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.root = getPreferenceManager().createPreferenceScreen(
SettingsActivity.this);
this.setPreferenceScreen(createPreferenceHierarchy());
}
}
基本上,按钮点击添加的新联系人下次不会显示。我需要将它保存在某个地方吗?我做错了什么?
答案 0 :(得分:0)
是的,这正好反映了你在做什么。这是首选项活动,这意味着它是所有首选项的持久值。这并不意味着保存所有(数量)偏好。
每次重新访问此活动时,它都只会调用onCreate方法。从onCreate你的控件将转到createPreferenceHierarchy()。在此方法中,您只设置一个EditText首选项。仅表示一个Edittext首选项。单击“添加联系人”按钮时,只会将新的edittext首选项添加到首选项屏幕。因此,您的活动正在重新启动,并且根据您的createPreferenceHierarchy()方法的定义,只应将一个EditText首选项添加到您的首选项屏幕。
如果你想要所有的首选项而不是onCreate从sharedPreference获取Count。并且多次使用Preference Catagory添加EditText Preference。
更改ur方法因此,对于demo,请参阅此内容。
public class SettingsActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
public PreferenceScreen root;
@SuppressWarnings("deprecation")
public PreferenceScreen createPreferenceHierarchy() {
PreferenceCategory userDetails = new PreferenceCategory(this);
userDetails.setTitle("User Details");
userDetails.setKey("userdetails");
root.addPreference(userDetails);
EditTextPreference userName = new EditTextPreference(this);
userName.setTitle("User Name");
userName.setSummary("Please enter your full name");
userName.setDialogTitle("Please enter your name");
userName.setKey("userName");
userDetails.addPreference(userName);
EditTextPreference contactCount = new EditTextPreference(this);
contactCount.setTitle("Count");
contactCount.setEnabled(false);
contactCount.setKey("contactcount");
userDetails.addPreference(contactCount);
PreferenceCategory contact1 = new PreferenceCategory(this);
contact1.setTitle("Emergency Contact 1");
contact1.setKey("contact1");
contact1.setSummary("Add emergency contact details");
root.addPreference(contact1);
EditTextPreference contactName1 = new EditTextPreference(this);
contactName1.setTitle("Name");
contactName1.setSummary("Emergency Contact Name");
contactName1.setDialogTitle("Please enter the name");
contactName1.setKey("contactname1");
contact1.addPreference(contactName1);
ListView v = getListView();
Button button = new Button(this);
button.setText("Add Contact");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int count = Integer.parseInt(root.getPreferenceManager()
.getSharedPreferences().getString("count", "1"));
int nextCount = count + 1;
EditTextPreference contactName = new EditTextPreference(
SettingsActivity.this);
contactName.setKey("contactname" + nextCount);
contactName.setTitle("Email ID");
contactName.setSummary("Emergency Contact's Name");
PreferenceCategory preferenceCategory = new PreferenceCategory(
SettingsActivity.this);
preferenceCategory.setKey("contact" + nextCount);
preferenceCategory.setTitle("Emergency Contact" + nextCount);
preferenceCategory.setSummary("Add emergency contact details");
root.addPreference(preferenceCategory);
root.addPreference(contactName);
Editor editor = root.getPreferenceManager()
.getSharedPreferences().edit();
editor.putString("count", Integer.toString(++count));
editor.commit();
}
});
v.addFooterView(button);
return root;
}
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.root = getPreferenceManager().createPreferenceScreen(
SettingsActivity.this);
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int Count = Integer.parseInt(preferences.getString("count", "1"));
for (int i =0;i< Count ;i++){
this.setPreferenceScreen(createPreferenceHierarchy());
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
}