寻找一种最佳方法来避免在共享首选项中覆盖键值

时间:2018-12-25 13:41:39

标签: android sharedpreferences

我正在尝试创建一个使用“共享首选项”保存用户名和联系电话的应用程序。 我面临的问题是,当我尝试向其中添加新条目时,上一个条目已被覆盖,由于无法解决此问题,我陷入了困境。我尝试了2到3种在Stack Overflow上找到的解决方案,但都没有帮助。 请避免使用注释的代码,因为我也在尝试更多。

这是我从用户那里获取输入并将其保存到共享首选项的活动

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.insert_button:
            String NAME = name.getText().toString();
         //   String CITY = city.getText().toString();
           // String EMAIL = email.getText().toString();
            String CONTACT = contact.getText().toString();

            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(nameKey , NAME);
           // editor.putString(emailKey, EMAIL);
           // editor.putString(cityKey, CITY);
            editor.putString(contactKey, CONTACT);
            editor.commit();
            Toast.makeText(this, "Data Inserted Successfully", Toast.LENGTH_SHORT).show();
            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(500);
            name.setText("");
           // email.setText("");
           // city.setText("");
            contact.setText("");


            break;


        case R.id.view_button:

            sharedPreferences = getSharedPreferences(mainKey, MODE_PRIVATE);
            if (sharedPreferences.contains(nameKey)) {
                name.setText(sharedPreferences.getString(nameKey, "Default Name"));
            }
             /*
            if (sharedPreferences.contains(emailKey)) {
                email.setText(sharedPreferences.getString(emailKey, "Default Email"));
            }
            if (sharedPreferences.contains(cityKey)) {
                city.setText(sharedPreferences.getString(cityKey, "Default City"));
            }  */
            if (sharedPreferences.contains(contactKey)) {
                contact.setText(sharedPreferences.getString(contactKey, "Default Contact"));
            }

            break;

        case R.id.home_button:

            Intent intent = new Intent(getApplicationContext(), Home_Activity.class);
            startActivity(intent);
            finish();

            break;


    }

}

我正在其他活动中访问它。

    public void onClick(View view) {
            sharedPreferences = getSharedPreferences(mainKey, MODE_PRIVATE);
            if (sharedPreferences.contains(nameKey)) {
                name_tv.setText(sharedPreferences.getString(nameKey, "Default Name"));

            }
           /*
            if (sharedPreferences.contains(emailKey)) {
                email_tv.setText(sharedPreferences.getString(emailKey, "Default Email"));
            }
            if (sharedPreferences.contains(cityKey)) {
                city_tv.setText(sharedPreferences.getString(cityKey, "Default City"));
            }*/
            if (sharedPreferences.contains(contactKey)) {
                contact_tv.setText(sharedPreferences.getString(contactKey, "Default Contact"));

            }
            Toast.makeText(Home_Activity.this, "Recent Value Shown..!!", Toast.LENGTH_SHORT).show();
        }
    });

现在我需要循环遍历name_tv和contact_tv中的新插入数据,但是数据将被覆盖。 你们可以帮忙吗? 请给我建议一个最佳方法.. !!

1 个答案:

答案 0 :(得分:0)

您可以使用SharedPreferences putStringSet / getStringSet将相同的姓名和联系人对保持在一起,并使用分隔符将它们合并为单个字符串。

添加:

String nameAndContact = MAME + "|" + CONTACT; // you need to filter out your divider from the original strings
Set<String> info = sharedPreferences.getStringSet("info", new HashSet<String>());
info.add(nameAndContact);
sharedPreferences.edit().putStringSet("info" , info).apply();

阅读:

Set<String> info = sharedPreferences.getStringSet("info", new HashSet<String>());
for (String nameAndContact : info) {
    String[] parts = info.split("|");
    Log.i("pair: name=" + parts[0] + ", contact=" + parts[1]);
}