I build an app that does some calculations in MainActivity.java and stores the results in SQLite database. When the user presses a button, opens the GraphActivity.java with the corresponding GraphView. Maybe it is because I don't have so much experience with SQLite databases yet but I can't get the Graph's X-axis to show the date each result was stored.
So, given that the Insert method and and the Graph take place in separate activities, how can I get the stored dates to show as X-axis in the GraphActivity?
I followed the YouTube tutorial for GraphView date & SQLite but it has the storing and the Graph in the same activity. (https://www.youtube.com/watch?v=lSgK6-cKjmA&list=PLFh8wpMiEi88ojfNpavGpMB0dtP4mvEqa&index=16)
I tried to send the date variable with Intent EXTRAS but it forces you to the GraphActivity when the Save button is pressed. Then I tried getting the time in the GraphActivity, which works but shows the date in X-axis as continuous "1-1-1970" for every result. I used format to show the current date with timestamp but still it doesn't show the date each result was stored, just the current date for all.
MainActivity.java: Insert method for the Results and Date
public void insertData() {
try {
(...)
// Gets the time & date the results are stored
String timeStamp = new SimpleDateFormat("d M yy hh:mm").format(Calendar.getInstance().getTime());
// Create a ContentValues object where column names are the keys,
// and container attributes from the MainActivity are the values.
ContentValues values = new ContentValues();
values.put(DexameniEntry.COLUMN_CONTAINER_NAME, "TODO");
values.put(DexameniEntry.COLUMN_a, X1aInput);
values.put(DexameniEntry.COLUMN_DATE, timeStamp);
values.put(DexameniEntry.COLUMN_b, X1bInput);
values.put(DexameniEntry.COLUMN_c, X1cInput);
values.put(DexameniEntry.COLUMN_d, dX1StringDouble);
values.put(DexameniEntry.COLUMN_e, eX1StringDouble);
values.put(DexameniEntry.COLUMN_percent, percentX1fromDoubletoString);
(...)
} catch (NumberFormatException e) {
} catch (NullPointerException e) {
}
}
GraphActivity.java : Setting the GraphView
public class GraphActivity extends AppCompatActivity {
(...)
SimpleDateFormat sdf = new SimpleDateFormat("EEE d M yyyy h mm", Locale.US);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.graph_activity);
GraphView bigGraphX1 = findViewById(R.id.bigGraph);
mDbHelper = new ResultsDbHelper(this);
sqLiteDatabase = mDbHelper.getWritableDatabase();
bigGraphX1.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, isValueX);
}
}
});
(...)
EDIT The DbHelper.class (named ResultsDbHelper)
public class ResultsDbHelper extends SQLiteOpenHelper {
public static final String LOG_TAG = ResultsDbHelper.class.getSimpleName();
// Name of the database in a String type constant (to be referenced later)
private static final String DATABASE_NAME = "containerResults.db";
// Database version in a Integer type constant for updating the database
private static final int DATABASE_VERSION = 1;
// Constructor: Constructs a new instance of ResultsDbHelper.
// * @param is the context of the app
public ResultsDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Modify the onCreate method to have the table created on startup of the app
@Override
public void onCreate(SQLiteDatabase db) {
// Define the schema of the table: which columns will be created by the onCreate method.
// The whole schema creation is put in a String variable (CREATE_RESULTS_TABLE) for easy reference
String CREATE_RESULTS_TABLE = "CREATE TABLE " + DexameniEntry.TABLE_NAME + " ("
+ DexameniEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ DexameniEntry.COLUMN_CONTAINER_NAME + " TEXT NOT NULL, "
+ DexameniEntry.COLUMN_DATE + " TEXT NOT NULL, "
+ DexameniEntry.COLUMN_a + " REAL NOT NULL, "
+ DexameniEntry.COLUMN_b + " REAL NOT NULL, "
+ DexameniEntry.COLUMN_c + " REAL NOT NULL, "
+ DexameniEntry.COLUMN_d + " REAL NOT NULL, "
+ DexameniEntry.COLUMN_e + " REAL NOT NULL, "
+ DexameniEntry.COLUMN_percent + " REAL NOT NULL);";
// Execute the SQL Statement (Create the table)
db.execSQL(CREATE_RESULTS_TABLE);
}
/**
* This is called when the database needs to be upgraded.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
final String LOG_TAG = ("Upgrading the database from version " + oldVersion + " to " + newVersion);
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + DexameniEntry.TABLE_NAME);
// Recreate tables
onCreate(db);
}
}
I expect the X-axis to display the date each result was stored, so that all the values to show as LineSeries in the GraphView. But I can't seem to get the dates from the database to the Graph so the date is always the same for all, and the LineSeries is not coherent.
EDIT: Before posting the question I had began also looking for a way to put in the GraphActivity a database reference of the DATE column, inside the FormatLabel method. For example
@Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
return
sdf.format(new Date(**put here the DATE column variable so that the X-axis is populated the column's data**);
}else {
return super.formatLabel(value, isValueX);
}
I couldn't do this either because I don't know how to write this code...
EDIT: Picture of the issue
I don't know what else to do to solve this, can you please help me?
EDIT 2: GraphActivity after changes (It seems like the graph still isn't populated by the db data.Maybe problem with Saving the data in the db?idk..)