0

我有多个SharedPreferences,每个都存储一个int值。我之前已经成功创建了多个SharedPreferences,但我正在尝试一种稍微不同的方法。我只保留最高的 5 个值。我SharedPreferenceArrayList. 我在上面调用反向排序方法,然后我删除最后一个值(因为它是额外的)。然后我将每个索引放入SharedPreferences. 这是我所拥有的:

prefs1 = this.getSharedPreferences("key1", Context.MODE_PRIVATE);
int value1 = prefs1.getInt("number1", 0);

prefs2 = this.getSharedPreferences("key2", Context.MODE_PRIVATE);
int value2 = prefs2.getInt("number2", 0);

prefs3 = this.getSharedPreferences("key3", Context.MODE_PRIVATE);
int value3 = prefs3.getInt("number3", 0);

prefs4 = this.getSharedPreferences("key4", Context.MODE_PRIVATE);
int value4 = prefs4.getInt("number4", 0);

prefs5 = this.getSharedPreferences("key5", Context.MODE_PRIVATE);
int value5 = prefs5.getInt("number5", 0);

ArrayList<Integer> numList = new ArrayList<Integer>();
Collections.addAll(numList, value0, value1, value2, value3, value4, value5);
Collections.sort(numList, Collections.reverseOrder());
numList.remove(numList.size()-1);
value1 = numList.get(0);
value2 = numList.get(1);
value3 = numList.get(2);
value4 = numList.get(3);
value5 = numList.get(4);

Editor editor = prefs1.edit();
editor.putInt("number1", value1);
editor.commit();

editor = prefs2.edit();
editor.putInt("number2", value2);
editor.commit();

editor = prefs3.edit();
editor.putInt("number3", value3);
editor.commit();

editor = prefs4.edit();
editor.putInt("number4", value4);
editor.commit();

editor = prefs5.edit();
editor.putInt("number5", value5);
editor.commit();

我遇到的问题是,即使在value0执行完成后为正之后,我的其他活动中的每个值都显示为 0。

我这样做有什么问题吗?(如果不是,那一定是当我在另一项活动中获得这些值时,但我几乎肯定我有这个权利。)

编辑*

也许是在检索中,这里来自检索活动:

prefs1 = this.getSharedPreferences("key1", Context.MODE_PRIVATE);
num1 = prefs1.getInt("number1", 0); //0 is the default value
tv1 = (TextView) findViewById(R.id.val1);
tv1.setText(String.valueOf(num1));

prefs2 = this.getSharedPreferences("key2", Context.MODE_PRIVATE);
num2 = prefs2.getInt("number2", 0); //0 is the default value
tv2 = (TextView) findViewById(R.id.val2);
tv2.setText(String.valueOf(num2));

prefs3 = this.getSharedPreferences("key3", Context.MODE_PRIVATE);
num3 = prefs3.getInt("number3", 0); //0 is the default value
tv3 = (TextView) findViewById(R.id.val3);
tv3.setText(String.valueOf(num3));

prefs4 = this.getSharedPreferences("key4", Context.MODE_PRIVATE);
num4 = prefs4.getInt("number4", 0); //0 is the default value
tv4 = (TextView) findViewById(R.id.val4);
tv4.setText(String.valueOf(num4));

prefs5 = this.getSharedPreferences("key5", Context.MODE_PRIVATE);
num5 = prefs5.getInt("number5", 0); //0 is the default value
tv5 = (TextView) findViewById(R.id.val5);
tv5.setText(String.valueOf(num5));
4

1 回答 1

4

您可以将 SharedPreferences 视为您所有东西的巨型地图,但除非您正在做任何时髦的事情,否则您应该从同一个巨型地图(即相同的 SharedPreferences)存储和检索数据。您正在做的是创建命名的共享首选项。我建议只使用默认值。如果您想了解更多关于这意味着什么的信息,请查看这个问题。

因此,在您的情况下,如果您要存储 5 个值,则可以SharedPreferences通过提供不同的键在同一个值内执行此操作,如下所示:

SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());  
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();

mSharedPreferencesEditor.putInt("numberX", numberX);
mSharedPreferencesEditor.putInt("numberY", numberY);
mSharedPreferencesEditor.commit()

mSharedPreferences.getInt("numberX", numberX);
mSharedPreferences.getInt("numberY", numberY);

您可以轻松地get使用put不同的键进入相同的 sharedPrefs。您的代码将变为:

SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());  
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();

int value1 = mSharedPreferences.getInt("number1", 0);
int value2 = mSharedPreferences.getInt("number2", 0);
int value3 = mSharedPreferences.getInt("number3", 0);
int value4 = mSharedPreferences.getInt("number4", 0);
int value5 = mSharedPreferences.getInt("number5", 0);

...

mSharedPreferencesEditor.putInt("number1", value1);
mSharedPreferencesEditor.putInt("number2", value2);
mSharedPreferencesEditor.putInt("number3", value3);
mSharedPreferencesEditor.putInt("number4", value4);
mSharedPreferencesEditor.putInt("number5", value5);
mSharedPreferencesEditor.commit();

您关于所有值都为 0 的问题可能会有所不同。我完全希望您对 getInt 的所有调用都为 0,因为您在 sharedPrefs 中永远不会存储除 0 之外的任何内容。看起来您只是在添加零并放置零。我不确定您在这里尝试做什么,但在调用此函数之前的某个时间点,通过调用mSharedPreferencesEditor.putInt(number);sharedPrefs 对象将数字 1-5 分配给 0 以外的值肯定会有所帮助,如下所示:

SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());  
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();

//for example, for the key "number5"
mSharedPreferencesEditor.putInt("number5", value5);
于 2013-02-20T02:32:46.603 回答