0

我需要一个包含多个联系方式的首选项屏幕。所以我默认有 1 个和一个按钮“添加联系人”来添加其他联系人。出于某种原因,一切都很好,但是当我再次访问设置时,新的偏好没有出现。

当我添加联系人时,所有首选项都会显示 - 并且可以输入数据。但是当我退出并重新输入首选项时,一切都消失了。只有联系人 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());
    }
}

所以基本上,通过按钮单击添加的新联系人下次不会出现。我需要把它保存在某个地方吗?我究竟做错了什么?

4

1 回答 1

0

是的,这正是你在做什么的反映。这是偏好活动,这意味着它会为所有偏好保留值。这并不意味着保存所有(数量的)首选项。

每次您重新访问此活动时,它只会调用 onCreate 方法。从 onCreate 你的控件将转到 createPreferenceHierarchy()。在此方法中,您仅设置一个 EditText 首选项。意味着只有一个 Edittext 首选项。当您单击添加联系人按钮时,只会将新的 edittext 首选项添加到首选项屏幕。因此,您的活动正在重新启动,并且根据您的 createPreferenceHierarchy() 方法的定义,您的首选项屏幕中只应添加一个 EditText 首选项。

如果你想要所有的首选项而不是 onCreate 从 sharedPreference 中获取计数。并且多次添加带有 Preference Catagory 的 EditText Preference。

相应地更改您的方法,对于演示,请参阅此。

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

    }
}
于 2013-09-10T19:33:59.147 回答