I lost data from my graph when I turn off app. How to save array with data for next usage? I am also new in Android app programming. I had created class with name XYvalues so don't be confused, that class is not important now for us I think.
Code:
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();
}
}
You need to store the graph data before your app is closed and read the data back from storage next time your app is started.
How to best store the data depends on how much data it is.
If its only a little data I suggest using the Preference API .
Other storage options can be found here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.