gpt4 book ai didi

java - 关闭 Android 应用程序后丢失数据

转载 作者:行者123 更新时间:2023-11-29 02:36:51 27 4
gpt4 key购买 nike

当我关闭应用程序时,我丢失了图表中的数据。如何保存数组以供下次使用?我也是 Android 应用程序编程的新手。我创建了名为 XYvalues 的类,所以不要混淆,我认为该类现在对我们来说并不重要。

代码:

public class graph extends AppCompatActivity {
private static final String TAG = "graph";
PointsGraphSeries<DataPoint> xySeries;
private Button btnAddPt;
private EditText mX,mY;
GraphView mScatterPlot;
private ArrayList <XYValues> xyValuesArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
btnAddPt = (Button) findViewById(R.id.BtnAddPt);
mX = (EditText) findViewById(R.id.numX);
mY = (EditText) findViewById(R.id.numY);
mScatterPlot = (GraphView) findViewById(R.id.scatterPlot);
xyValuesArray = new ArrayList<>();
init();
}
private void init() {
xySeries = new PointsGraphSeries<>();
btnAddPt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!mX.getText().toString().equals("") && !mY.getText().toString().equals("")) {
double x = Double.parseDouble(mX.getText().toString());
double y = Double.parseDouble(mY.getText().toString());
Log.d(TAG, "onClick: Adding a new point. (x,y)(" + x + "," + y + ")");
xyValuesArray.add(new XYValues(x, y));
init();
} else


{
toastMessage("You must fill both fields");
}
}


});

if (xyValuesArray.size() != 0) {
createScatterPlot();

} else {
Log.d(TAG, "onCreate: No data to plot");

}
}
private void createScatterPlot(){
Log.d(TAG, "createScatterPlot:Creating scatter plot.");
xyValuesArray = sortArray(xyValuesArray);
for (int i =0; i<xyValuesArray.size();i++){
try{
double x = xyValuesArray.get(i).getX();
double y = xyValuesArray.get(i).getY();
xySeries.appendData(new DataPoint(x,y),true,1000);

}catch (IllegalArgumentException e){


Log.e(TAG, "createScatterPlot:IllegalArgumentException: "+e.getMessage());
}
}
xySeries.setShape(PointsGraphSeries.Shape.POINT);
xySeries.setColor(Color.BLUE);
xySeries.setSize(20f);

mScatterPlot.getViewport().setScalable(true);
mScatterPlot.getViewport().setScalableY(true);
mScatterPlot.getViewport().setScrollable(true);
mScatterPlot.getViewport().setScrollableY(true);

mScatterPlot.getViewport().setYAxisBoundsManual(true);
mScatterPlot.getViewport().setMaxY(250);
mScatterPlot.getViewport().setMinY(0);

mScatterPlot.getViewport().setXAxisBoundsManual(true);
mScatterPlot.getViewport().setMaxX(3000);
mScatterPlot.getViewport().setMinX(0);

mScatterPlot.addSeries(xySeries);
}

private ArrayList<XYValues> sortArray(ArrayList<XYValues>array){
int factor = Integer.parseInt(String.valueOf(Math.round(Math.pow(array.size(),2))));
int m = array.size() - 1;
int count =0;
Log.d(TAG,"sortArray:Sorting XYarray");
while (true){
m--;
if(m<=0){
m=array.size()-1;
}
Log.d(TAG,"SortArray: m =" +m);
try{
double tempY = array.get(m-1).getY();
double tempX = array.get(m-1).getX();
if (tempX>array.get(m).getX()){
array.get(m-1).setY(array.get(m).getY());
array.get(m).setY(tempY);
array.get(m-1).setX(array.get(m).getX());
array.get(m).setX(tempX);
}
else if (tempX ==array.get(m).getX()){
count++;
Log.d(TAG,"sortArray: count = "+count);

}
else if (array.get(m).getX()>array.get(m-1).getX()){
count++;
Log.d(TAG,"sortArray: count = "+count);
}
if (count == factor){
break;
}
}catch (ArrayIndexOutOfBoundsException e){
Log.e(TAG,"sortArray: ArrayIndexOutBoundsException. Need more than 1 data to create plot = "+e.getMessage());
break;
}
}
return array;
}
private void toastMessage (String message){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}

}

最佳答案

您需要在您的应用关闭之前存储图形数据,并在您的应用下次启动时从存储中读回数据。

如何最好地存储数据取决于数据量。

如果只有少量数据,我建议使用 Preference API .

可以找到其他存储选项 here .

关于java - 关闭 Android 应用程序后丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com