【发布时间】:2019-07-05 17:54:41
【问题描述】:
我正在构建一个应用程序,它选择日期之间的行,日期为 dd/mm/yyyy 格式,并计算状态为挂起、注册和拒绝的行。我已经做了一些工作,但它不起作用。我将日期作为文本存储在数据库中。我在下面粘贴了代码。
public void showMonthlyPopUp(View view) {
weeklyDialog.setContentView(R.layout.pop_up_all_list);
TextView nameTextView = weeklyDialog.findViewById(R.id.textView);
nameTextView.setText("MONTHLY");
TextView pendingTextView = weeklyDialog.findViewById(R.id.textView6);
TextView signUpTextView = weeklyDialog.findViewById(R.id.textView3);
TextView rejectedTextView = weeklyDialog.findViewById(R.id.textView7);
Button shareButton = weeklyDialog.findViewById(R.id.button_share);
String[] projection = {
InfoContract.InfoEntry._ID,
InfoContract.InfoEntry.COLUMN_STATUS,
InfoContract.InfoEntry.COLUMN_DATE
};
Calendar calendar = Calendar.getInstance();
String strDate = calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
int dayInt = calendar.get(Calendar.DAY_OF_MONTH);
String[] selectionArgs = new String[dayInt];
for (int i = 1; i <= dayInt; i++) {
selectionArgs[i - 1] = i + "/" + strDate;
}
String selection = InfoContract.InfoEntry.COLUMN_DATE + " =?";
for (int i = 1; i < dayInt; i++) {
selection += " OR " + InfoContract.InfoEntry.COLUMN_DATE + " =?";
}
Cursor cursor = this.getContentResolver().query(InfoContract.InfoEntry.CONTENT_URI, projection, selection, selectionArgs, null);
int pending = 0;
int signUp = 0;
int rejected = 0;
while (cursor.moveToNext()) {
int statusColumnIndex = cursor.getColumnIndex(InfoContract.InfoEntry.COLUMN_STATUS);
int status = cursor.getInt(statusColumnIndex);
if (status == InfoContract.InfoEntry.STATUS_SIGN_UP) signUp = signUp + 1;
else if (status == InfoContract.InfoEntry.STATUS_REJECTED) rejected++;
else pending++;
}
cursor.close();
pendingTextView.setText("" + pending);
signUpTextView.setText("" + signUp);
rejectedTextView.setText("" + rejected);
weeklyDialog.show();
final int finalPending = pending;
final int finalSignUp = signUp;
final int finalRejected = rejected;
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
shareData(finalPending, finalSignUp, finalRejected, "Monthly Details: ");
}
});
}
【问题讨论】:
-
将列中日期的格式改为YYYY-MM-DD。这是 SQLite 唯一可比较的日期格式。任何其他格式都需要字符串操作并且使 sql 代码复杂化。
-
如何从数据库中选择多个日期。我需要获取整个月的原始数据,例如,如果今天的日期是 7,我需要从第 1 天到第 7 天。
标签: java android android-sqlite android-contentprovider