繁体   English   中英

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

[英]Lost data after turn off Android application

关闭应用程序后,我的图形数据丢失了。 如何保存数据数组以备下次使用? 我也是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

其他存储选项可在此处找到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM