Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 432
user1275331 Avatar asked Mar 23 '26 21:03

user1275331


1 Answers

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.

like image 84
phlogratos Avatar answered Mar 26 '26 09:03

phlogratos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!