1

我一直在使用 GraphView 库。ArrayList 中有数据存储在 SharedPreferences onStop() 中。当调用我的 Intent / 类来绘制图形时,它会从 SharedPreferences 读取先前存储的数据并将其保存到将用于绘制图形的数组中。

但图表看起来像这样:http: //i.stack.imgur.com/12j5j.jpg

添加到我的 ArrayList 的唯一数据是 14。我已经对 Array Lists 进行了一些研究,发现 10 是初始容量。也许这就是问题所在?

在我的主要活动中恢复 ArrayList 的代码:

SharedPreferences arrayGraph = getSharedPreferences("arrayGraph_temp_1", 0);
int arraySize = arrayGraph.getInt("arrayGraph_temp_1_size", 0);

for (int i = 0; i < arraySize; i++) {

    float temp = arrayGraph.getFloat("temp_" + i, 0);
    arrayGraph_temp_1.add(i, temp);
}

将浮点变量添加到 ArrayList 的代码:

arrayGraph_temp_1.add(arraySize, x);

将 ArrayList 保存到 SharedPreferences 的代码:

SharedPreferences arrayGraph = getSharedPreferences("arrayGraph_temp_1", 0);  
SharedPreferences.Editor editor = arrayGraph.edit();                          

int listSize = arrayGraph_temp_1.size();
editor.putInt("arrayGraph_temp_1_size", listSize);

for (int i = 0; i < listSize; i++) {

    float temp = arrayGraph_temp_1.get(i);
    editor.putFloat("temp_" + i, temp);
}

editor.commit();

在类中恢复数据以绘制图形的代码:

SharedPreferences arrayGraph = getSharedPreferences("arrayGraph_temp_1", 0);
int arraySize = arrayGraph.getInt("arrayGraph_temp_1_size", 0);

GraphView.GraphViewData[] data;
data = new GraphView.GraphViewData[arraySize];

for (int i = 0; i < arraySize; i++) {

    float temp = arrayGraph.getFloat("temp_" + i, 0);
    data[i] = new GraphView.GraphViewData(i, temp);

}

有任何想法吗?

4

0 回答 0