简体   繁体   English

Android:SharedPreferences问题(多个SharedPreferences)

[英]Android: SharedPreferences Issue (Multiple SharedPreferences)

I have multiple SharedPreferences that are each storing one int value each. 我有多个SharedPreferences ,每个SharedPreferences存储一个int值。 I have successfully created multiple SharedPreferences before, but I am trying a slightly different approach for this one. 之前我已经成功创建了多个SharedPreferences ,但是我正在尝试一种稍微不同的方法。 I am only keeping the highest 5 values. 我只保留最高的5个值。 I am getting each value from its SharedPreference , then I am adding the 5 values + the current value I want to compare against the others in an ArrayList . 我从其SharedPreference获取每个值,然后将5个值+当前值与要比较的当前值与ArrayList中的其他值进行比较。 I am calling the reverse sort method on it, then I am removing the last value (because it is extra). 我在上面调用反向排序方法,然后删除了最后一个值(因为它是多余的)。 I am then putting each index into the editor of the SharedPreferences . 然后,我将每个索引放入SharedPreferences的编辑器中。 Here is what I have: 这是我所拥有的:

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();

The problem I am having is that is showing 0s for each one of the values in my other activity even after value0 is positive after it executed through. 我遇到的问题是,即使在执行value0为正后,我的其他活动中的每个值也都显示0。

Is there anything wrong with how I am doing this? 我这样做有什么问题吗? (If not then it must be when I get these values in another activity, but I am almost positive I have that right.) (如果不是,那么一定是在其他活动中获得这些值时,但我几乎肯定我拥有这项权利。)

EDIT* 编辑*

Perhaps it is in the retrieval, here is from the retrieving activity: 也许是在检索中,这是在检索活动中:

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));

You can think of SharedPreferences like a giant map for all your stuff, but unless you are doing anything funky, you should store and retrieve data from the same giant map (ie the same SharedPreferences). 您可以将SharedPreferences视为所有内容的巨型地图,但是除非您做任何时髦的事情,否则您应该从同一巨型地图(即,相同的SharedPreferences)存储和检索数据。 What you are doing is creating named shared preferences. 您正在做的是创建命名的共享首选项。 I would recommend just using the default. 我建议只使用默认值。 If you are curious to learn more about what that means check out this question. 如果您想进一步了解这意味着什么, 请查看此问题。

So, in your case if you are storing 5 values, you can do so within the same SharedPreferences by just supplying different keys as follows: 因此,在您的情况下,如果要存储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);

You can easily get and put with different keys into the same sharedPrefs. 您可以轻松地getput使用不同的密钥到同一sharedPrefs。 Your code would become: 您的代码将变为:

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();

Your issue concerning all your values being 0 may be different. 关于所有值均为0的问题可能有所不同。 I would fully expect all your calls to getInt to be 0 because you are never storing anything but 0 in the sharedPrefs. 我完全希望您对getInt的所有调用都为0,因为您永远不会在sharedPrefs中存储除0以外的任何内容。 It looks like you are just adding zeros and putting zeros. 看起来您只是在添加零并放入零。 I am not sure what you are trying to do here, but it sure would help to, at some point before calling this function, assign numbers 1-5 to something other than 0 by calling mSharedPreferencesEditor.putInt(number); 我不确定您要在此处执行的操作,但是在调用此函数之前的某个时候,通过调用mSharedPreferencesEditor.putInt(number);将数字1-5分配给除0之外的其他值,这肯定会有所帮助mSharedPreferencesEditor.putInt(number); on a sharedPrefs object like so: 在sharedPrefs对象上,如下所示:

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

//for example, for the key "number5"
mSharedPreferencesEditor.putInt("number5", value5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM