Reputation: 1
I have a database that stores the values, a graphview line graph, and a dataseries. The current date is the X value and user inputs their current weight for the Y value. Once the user clicks add the data is supposed to get added to the graph however my array is not sorting. I keep getting error messages. Not sure if it's in the wrong place or if I'm missing something.
Error Message: The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.
// Get graph from layout graph = (GraphView) findViewById(R.id.graph);
UserDB = new DBHelper(this);
sqLiteDatabase = UserDB.getWritableDatabase();
dataSeries = new LineGraphSeries<>(new DataPoint[0]);
insertDataToGraph();
graph.addSeries(dataSeries);
graph.getGridLabelRenderer().setNumHorizontalLabels(3);
Calendar calendar = Calendar.getInstance();
String dateTXT = sdf.format(calendar.getTime());
date.setText(dateTXT);
public void insertDataToGraph(){
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long xValue = new Date().getTime();
int currentWeightTXT = parseInt(currentWeight.getText().toString());
UserDB.insertToData(xValue, currentWeightTXT);
dataSeries.resetData(grabData());
graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){
@Override
public String formatLabel (double value, boolean isValueX){
if(isValueX){
return sdf.format(new Date((long) value));
}
else {
return super.formatLabel(value, false);
}
}
});
}
});
}
private DataPoint[] grabData(){
String [] column = {"xValue", "currentWeight"};
@SuppressLint("Recycle") Cursor cursor = sqLiteDatabase.query("Graph", column, null, null, null, null, null);
DataPoint[] dataPoints = new DataPoint[cursor.getCount()];
for(int i = 0; i < cursor.getCount(); i++){
cursor.moveToNext();
dataPoints[i] = new DataPoint(cursor.getLong(0), cursor.getInt(1));
}
Arrays.sort(dataPoints);
return dataPoints;
}
Upvotes: 0
Views: 173
Reputation: 176
There should be an argument called orderBy
for the method query
. You should set it to "xValue"
. This way the database handles the sorting and you don't need to do it in your JAVA code.
Upvotes: 0