user1275331
user1275331

Reputation: 33

Android Integer Arrays

Hello I am new to Android development and I decided to work with the AndroidPlot library. To create a graph I need to enter in a number array like this

Number[] seriesOfNumbers = {4, 6, 3, 8, 2, 10};

What I need help with is creating that data in my app. My app runs a service once everyday and I want it to collect a certain number and add it to this array. Say for example something like this..

ArrayList<Integer> seriesOfNumbers = new ArrayList<Integer>();
seriesOfNumbers.add(5);
// Save the array

and then the next day retrieve this array and add another number to it and so on. Ive read that I should use SQLite but I am storing only one number each day. I cant create a new Array everyday because i need data from the previous days. What is the proper way to do this? Thanks

Edit:

This is as far as I got

public static void saveArray(Context ctx) 
{

    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(ctx);

    SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences
            .edit();


    Number[] list = new Number[10];

    StringBuilder str = new StringBuilder();

    for (int i = 0; i < list.length; i++) 
    {

        str.append(list[i]).append(",");

    }


    sharedPreferencesEditor.putString("string", str.toString());

    sharedPreferencesEditor
            .commit();

}

public void getArray(Context ctx)
{
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String savedString = prefs.getString("string", "1");

    StringTokenizer st = new StringTokenizer(savedString, ",");

    for (int i = 0; i < 1; i++) 
    {
        array[i] = Integer.parseInt(st.nextToken());

    }


}

What I would like to do is be able to pass an integer through saveArray(Context ctx) and have it added to an array. Then it gets parsed into a string to be stored into shared preferences and then retrieved by getArray(Context ctx) where it gets recreated into an array if that makes any sense. Any help is very much appreciated Note: above code causes FC

Upvotes: 3

Views: 32333

Answers (1)

phlogratos
phlogratos

Reputation: 13924

Try something like this:

ArrayList<Integer> seriesOfNumbers = existsList() ? loadList() : new ArrayList<Integer>();
seriesOfNumbers.add(5);
saveList(seriesOfNumbers);

You just have to implement the ...List() - methods, maybe by using SqLite.

Upvotes: 3

Related Questions