Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9316335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:35:24+00:00 2026-06-19T02:35:24+00:00

I have multiple SharedPreferences that are each storing one int value each. I have

  • 0

I have multiple SharedPreferences that are each storing one int value each. I have successfully created multiple SharedPreferences before, but I am trying a slightly different approach for this one. I am only keeping the highest 5 values. 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. 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. 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.

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));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:35:26+00:00Added an answer on June 19, 2026 at 2:35 am

    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 (i.e. the same 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:

    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. 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. I would fully expect all your calls to getInt to be 0 because you are never storing anything but 0 in the sharedPrefs. 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); on a sharedPrefs object like so:

    SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());  
    SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();
    
    //for example, for the key "number5"
    mSharedPreferencesEditor.putInt("number5", value5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple tables . I have created one stored procedure where I am
I have multiple objects that inherit from a base class and am trying to
I have multiple classes (controllers) that share a huge chunk of code with each
I have multiple XAML TextBox es, each of which manipulate a corresponding value in
I have multiple tabs that each does something that may interfere with stuff going
I have multiple input fields on one page. Each input field have a text
I have multiple UI components that needs a navigation menu. I am trying to
i have multiple images that are created dynamically, and i want in some cases
I have multiple file names that I need to parse. One commonality between these
I have multiple UpdatePanels on a webpage, one inside UserControl (Purple ColorBox as show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.