Android 整数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12415634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Integer Arrays
提问by user1275331
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
您好,我是 Android 开发新手,我决定使用 AndroidPlot 库。要创建图形,我需要输入这样的数字数组
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
然后第二天检索这个数组并添加另一个数字,依此类推。我读过我应该使用 SQLite,但我每天只存储一个数字。我不能每天创建一个新数组,因为我需要前几天的数据。这样做的正确方法是什么?谢谢
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
我想做的是能够通过 saveArray(Context ctx) 传递一个整数并将其添加到数组中。然后它被解析为一个字符串以存储到共享首选项中,然后由 getArray(Context ctx) 检索,如果有任何意义,它将被重新创建到一个数组中。非常感谢任何帮助注意:上面的代码会导致 FC
回答by phlogratos
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.
你只需要实现 ...List() - 方法,也许通过使用 Sqlite。