0

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?

rtindru
  • 5,107
  • 9
  • 41
  • 59
  • Somebody please tell me if I have a conceptual mistake, or what is wrong with my implementation? – rtindru Aug 26 '13 at 10:05

1 Answers1

0

yes this is exactly reflection of what you are doing. This is preference activity which means it is persisting values for all the preferences. That doesn't mean to save all the(number of ) preferences.

Every time you revisit this activity, it will call onCreate method only. From onCreate ur control will go to createPreferenceHierarchy(). In this method you are setting up one EditText preference only. Means only one Edittext preference. When you click on add Contact button , than only new edittext preference is being added to preference screen. So you activity is restarting , and as per defination of ur createPreferenceHierarchy() method , only one EditText preference should be added to your Preference Screen.

If u want all the Preferences back than onCreate get the Count from sharedPreference. and that many times add EditText Preference with Preference Catagory.

Change ur method Accordingly , for demo see this.

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

    }
}
Pranav Jadav
  • 763
  • 5
  • 11