3

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

1
  • It sounds like you might need more than just one previous day's numbers, correct? If so, Sqlite would be ideal. Just "insert" a new row each day, consisting of the numbers and the date. You can then "select" any row from any day as needed. Here's a great tutorial: vogella.com/articles/AndroidSQLite/article.html Commented Sep 13, 2012 at 22:29

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.