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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:07:58+00:00 2026-06-12T16:07:58+00:00

I have found a great tutorial about storing arrays in SharedPreferences. Every time I

  • 0

I have found a great tutorial about storing arrays in SharedPreferences. Every time I add one item to an array, I store it in SharedPreferences, but when loading the array I get null for all values. Size of the array is increased by 1 every time, but the elements are stored as null. Why?

String tname[] = new String[loadArray(t_name, this).length + 1];
for (int i=0; i<tname.length; i++)
{
    Log.i("tname[" + i + "]", tname[i] + "");  //null
}
tname[tname.length-1] = et_name;  //this is a string which is not null
saveArray(tname, t_name, NewSchedule.this);

public boolean saveArray(String[] array, String arrayName, Context mContext) {   
        SharedPreferences prefs = mContext.getSharedPreferences("sp_name", 0);  
        SharedPreferences.Editor editor = prefs.edit();  
        editor.putInt(arrayName +"_size", array.length);  
        for(int i=0;i<array.length;i++)  
            editor.putString(arrayName + "_" + i, array[i]);  
        return editor.commit();  
    } 

    public String[] loadArray(String arrayName, Context mContext) {  
        SharedPreferences prefs = mContext.getSharedPreferences("sp_name", 0);  
        int size = prefs.getInt(arrayName + "_size", 0);  
        String array[] = new String[size];  
        for(int i=0;i<size;i++)  
            array[i] = prefs.getString(arrayName + "_" + i, null);  
        return array;  
    }  

Am I using the found code the right way?

**SOLUTION based ony 10s’ answer:

String tname[] = new String[loadArray(t_name, this).length + 1];
String[] prevPrefs = loadArray(t_name, this);
for(int i=0; i<prevPrefs.length; i++) {
   tname[i] = prevPrefs[i];  //0th element will be null, as well as e.g. the 4th when there are 3 elements
}
for (int i=0; i<tname.length; i++)
{
    if (tname[i] == null) {  //so we put the new value in place of the null
        tname[i] = et_name;
    }
}

//Arraylists are much handier than arrays
if (arr_names.size() > 0) arr_names.clear(); //first clear all elements
for (int i=0; i<tname.length; i++) {
   if (tname[i] != null) arr_names.add(tname[i]);
}
for (int i=0; i<arr_names.size(); i++) {
    if (arr_names.get(i) == null) {
        arr_names.remove(i);
        }
}

String[] tname2 = new String[arr_names.size()]; //create new array
for (int i=0; i<tname2.length; i++)  //popluate new array
{
    tname2[i] = arr_names.get(i);
}

saveArray(tname2, t_name, NewSchedule.this);
  • 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-12T16:08:00+00:00Added an answer on June 12, 2026 at 4:08 pm

    The problem is here:

    String tname[] = new String[loadArray(t_name, this).length + 1];
    

    This line does not create an array that is filled up with values: the loadArray(t_name, this).length + 1 actually returns a number. All this is actually translated as:

    String tname[]= new String[5];
    

    which actually creates a new array of Strings that has null values.

    Solution: you should then iterate among the items of a temporary prevPrefs[] that is loaded from the loadArray() method of yours and add them in the tname[] like this:

    String[] prevPrefs = loadArray(t_name, this);
    for(int i=0; i<prevPrefs.length; i++) {
       tname[i] = prevPrefs[i];
    }
    

    then you would have the array filled up with the values of the previous one and you can add to the last empty spot the new value as you already did.

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

Sidebar

Related Questions

I have found this great tutorial, about uploading files with a Flex app, using
I have just found this great tutorial as it is something that I need.
I have adapted some code from a great article I found about circle drawing
I'm trying to learn jQuery and Ajax and have found a great tutorial to
I have found a great tutorial on how to show and hide a certain
I have found this tutorial , which looks great, except the fact, that I
I have found many great answers to this from a year ago (when it
I have problem with text drawing around Circle. I found great sample in C#
I've been reading up on SqlTransactions and have found a great load of examples
I found some great example code some months ago to add a compose button

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.