I need a preference screen with multiple contact details. So I have 1 by default and a button "Add Contact" to add additional contacts. For some reason, it all works well, but the new preference does not show up when I visit the settings again.
When I add a contact, all the preferences show up - and the data can be entered. But when I exit and reenter prefs, its all gone. Only contact 1 shows up.
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());
}
}
So basically, the new contact added by the button click does not show up the next time. Do i need to save this somewhere. What am I doing wrong?