【问题标题】:Lost data after turn off Android application关闭Android应用程序后丢失数据
【发布时间】:2018-03-12 19:02:05
【问题描述】:

我在关闭应用时丢失了图表中的数据。如何使用数据保存数组以供下次使用?我也是 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();
}

}

【问题讨论】:

  • 您需要学习如何使用 Preferences API(可能还有 Parcelable API)。它不是太复杂,但如果您需要在 Android 中存储持久数据,它是最低限度的。
  • 尝试对这个领域进行一点研究。io/docs/java/latest 它真的很简单而且很快。或者尝试序列化。

标签: java android arrays arraylist graph


【解决方案1】:

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

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

如果只是一点点数据,我建议使用Preference API

其他存储选项可以在here找到。

【讨论】:

  • 尝试对这个realm.io/docs/java/latest进行一点研究,它真的很简单而且很快。或者尝试序列化。
  • @trip08:欢迎您发布您的链接作为对另一个答案的回复,但是在他/她的问题下方向问题作者提出不是更好吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 2017-07-11
  • 2016-04-24
  • 1970-01-01
相关资源
最近更新 更多