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 3978228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:01:18+00:00 2026-05-20T05:01:18+00:00

Im using the SharedPreferences in my app to retain the ability to save/retrieve string

  • 0

Im using the “SharedPreferences” in my app to retain the ability to save/retrieve string values from multiple edittext boxes, and thats working just fine. I also have a Spinner in my activity with a string-array for it’s usable values. But I am unclear on how to write the spinners selection to the SharedPreferences, then read the SharedPreferences later to retireve and set it’s value.

Here is the configuration I have for the edittext’s:

-Button to activate save values to SharedPreferences-

public void buttonSaveSendClick(View view) {

    SharedPreferences.Editor editor = getPreferences(0).edit();

    EditText editTextCallId = (EditText) findViewById(R.id.editTextCallId);
    editor.putString("editTextCallIdtext", editTextCallId.getText().toString());
    editor.putInt("selection-startCallId", editTextCallId.getSelectionStart());
    editor.putInt("selection-endCallId", editTextCallId.getSelectionEnd());
    editor.commit();
}

-Button to activate restore last saved values from SharedPreferences-

public void buttonRestoreLastClick(View view) {

    SharedPreferences prefs = getPreferences(0); 

    EditText editTextCallId = (EditText) findViewById(R.id.editTextCallId);
    String editTextCallIdtextrestored = prefs.getString("editTextCallIdtext", null);
    editTextCallId.setText(editTextCallIdtextrestored, EditText.BufferType.EDITABLE);
    int selectionStartCallId = prefs.getInt("selection-startCallId", -1);
    int selectionEndCallId = prefs.getInt("selection-endCallId", -1);
    editTextCallId.setSelection(selectionStartCallId, selectionEndCallId);
}

Any suggestions on how to construct the collection of the spinner’s selected value in the first button (save)? Then how to return that saved value to the spinners view on press of the “restore” button?

  • 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-05-20T05:01:18+00:00Added an answer on May 20, 2026 at 5:01 am

    You have to call editor.apply(); once after all of your editor.put(); statements. Otherwise all of the changes that you’ve made to the preferences will get discarded. Assuming that the items in your array won’t be changing position at all then you can just store the selected position as an int in your preferences.

    to Save:

    int selectedPosition = yourSpinner.getSelectedItemPosition();
    editor.putInt("spinnerSelection", selectedPosition);
    editor.apply();
    

    to Load:

    yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));
    

    If the items in your array are going to change then you’ll have to store the actual string, instead of the position. Something like this would work:

    String selectedString = yourArray[yourSpinner.getSelectedItemPosition()];
    editor.putString("spinnerSelection", selectedString);
    editor.apply();
    

    and

    find the position of the string by looping through your array and checking array[i] against the value stored in prefs. Then call yourSpinner.setSelected(position). If you use an ArrayList instead this part could be done without the loop by calling

    ArrayList.indexOf(prefs.getString("spinnerSelection", ""));

    Be aware that only ArrayList does have an indexOf(); method. On a plain Array you can not use the indexOf(); method, you’ll have to manually search your Array to find the right value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app has the ability to backup and restore SharedPreferences using an BackupAgentHelper class.
I'm using SharedPreferences to save two kinds of preference information. 1) General app preferences
From several classes in my app I want to get String global values without
I'm using SharedPreferences to store some app settings. If I push a new version
I understand how to save an application's state by using SharedPreferences, onSavedInstanceState() & onRestoreInstanceState(),
I am using SharedPreferences in my android app. I am using both commit() and
Situation I have a fairly simple app with 2 layouts using SharedPreferences. Main.xml Settings.xml
I am trying to save the values from different checkboxes located in a layout.xml
I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
I am using following code to post image on Facebook wall. it's working fine.

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.