【发布时间】:2019-06-06 00:19:57
【问题描述】:
我构建了一个应用程序,它在 MainActivity.java 中进行一些计算并将结果存储在 SQLite 数据库中。当用户按下按钮时,会打开带有相应 GraphView 的 GraphActivity.java。也许是因为我对 SQLite 数据库还没有太多经验,但我无法让 Graph 的 X 轴显示每个结果的存储日期。
那么,鉴于 Insert 方法和 Graph 发生在不同的活动中,我怎样才能让存储的日期在 GraphActivity 中显示为 X 轴?
我遵循了 GraphView 日期和 SQLite 的 YouTube 教程,但它在同一活动中具有存储和图形。 (https://www.youtube.com/watch?v=lSgK6-cKjmA&list=PLFh8wpMiEi88ojfNpavGpMB0dtP4mvEqa&index=16)
我尝试使用 Intent EXTRAS 发送日期变量,但是当按下 Save 按钮时它会强制您进入 GraphActivity。 然后我尝试在 GraphActivity 中获取时间,它可以工作,但对于每个结果,X 轴上的日期显示为连续的“1-1-1970”。 我使用格式来显示带有时间戳的当前日期,但它仍然没有显示每个结果的存储日期,只是所有的当前日期。
MainActivity.java:结果和日期的插入方法
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 : 设置 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);
}
}
});
(...)
编辑 DbHelper.class(名为 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);
}
}
我希望 X 轴显示每个结果的存储日期,以便所有值在 GraphView 中显示为 LineSeries。但我似乎无法从数据库中获取日期到图表,所以日期总是相同的,并且 LineSeries 不连贯。
编辑:在发布问题之前,我还开始寻找一种方法,在 FormatLabel 方法中将 DATE 列的数据库引用放入 GraphActivity。例如
@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);
}
我也不能这样做,因为我不知道如何编写这段代码......
我不知道还有什么办法可以解决这个问题,你能帮帮我吗?
编辑 2:GraphActivity after changes(似乎图表仍未由 db 数据填充。在 db?idk 中保存数据可能存在问题..)
【问题讨论】:
-
您能否编辑您的问题以包含 ResultsDBHelper 类。
-
您在
new Date((long)value)中的值似乎太小了。顺便说一句,考虑不使用Date和SimpleDateFormat,这些类设计不佳且早已过时。更好的选择是将ThreeTenABP 添加到您的Android 项目中,以便您可以使用java.time, the modern Java date and time API。 -
X 轴上的日期为连续的“1-1-1970”,即开始日期,即 unix 纪元中的值 0。所以所有结果都为 0。使用 SQL 使用一种公认的日期格式在处理日期时有很多好处(它没有 DATE 类型,通常使用 TEXT 的列关联(尽管您可以存储在 unix 时间戳中)),例如YYYY-MM-DD HH:MM:SS 见Date And Time Functions
-
@MikeT 我包含了 ResultsDbHelper 类。我也为日期列使用了 TEXT 类型...
-
@OleV.V 你说的值太小是什么意思?我的意思是它们是日期而不是数字,比如 10,太小了。 (对不起,如果我不明白)。那么添加 ThreeTenABP 会解决问题还是一般建议?
标签: java sqlite date android-sqlite android-graphview