增加同日搜索
This commit is contained in:
parent
8586e313b2
commit
a5f45f3630
|
@ -2,7 +2,7 @@
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.hty.event"
|
package="com.hty.event"
|
||||||
android:versionCode="1"
|
android:versionCode="1"
|
||||||
android:versionName="1.6">
|
android:versionName="1.7">
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package com.hty.event;
|
package com.hty.event;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
@ -60,7 +63,7 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cursor query(long date_start, long date_end) {
|
public Cursor queryRange(long date_start, long date_end) {
|
||||||
db = getWritableDatabase();
|
db = getWritableDatabase();
|
||||||
Cursor c;
|
Cursor c;
|
||||||
if (date_start != date_end)
|
if (date_start != date_end)
|
||||||
|
@ -70,12 +73,40 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Cursor queryMonthDay(int month, int day) {
|
||||||
|
db = getWritableDatabase();
|
||||||
|
Cursor cursor = db.rawQuery("SELECT MAX(time) FROM " + TableName, null);
|
||||||
|
long time_max = -1;
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
time_max = cursor.getLong(0);
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
Date date = new Date(time_max);
|
||||||
|
int year_max = date.getYear();
|
||||||
|
cursor = db.rawQuery("SELECT MIN(time) FROM " + TableName, null);
|
||||||
|
long time_min = -1;
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
time_min = cursor.getLong(0);
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
date = new Date(time_min);
|
||||||
|
int year_min = date.getYear();
|
||||||
|
String selection = "";
|
||||||
|
for (int year=year_max; year>year_min; year--) {
|
||||||
|
date = new Date(year, month, day);
|
||||||
|
selection += "(time >= " + date.getTime() + " and time <= " + (date.getTime() + 24*60*60*1000) + ")";
|
||||||
|
if (year > year_min + 1) {
|
||||||
|
selection += " or ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Cursor c = db.query(TableName, null, selection, null, null, null, "time desc");
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
public void del(String id) {
|
public void del(String id) {
|
||||||
if (db == null)
|
if (db == null)
|
||||||
db = getWritableDatabase();
|
db = getWritableDatabase();
|
||||||
db.delete(TableName, "_id=?", new String[] { id });
|
db.delete(TableName, "_id=?", new String[] { id });
|
||||||
// Log.e("id", id + "");
|
|
||||||
// db.ExecuteNonQuery(CommandType.Text, "VACUUM");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -83,4 +114,5 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||||
if (db != null)
|
if (db != null)
|
||||||
db.close();
|
db.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -9,6 +9,7 @@ import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
@ -118,7 +119,7 @@ public class MainActivity extends Activity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
String[] sm = { "添加", "日期范围搜索", "分享数据库", "导出HTML", "导出CSV", "更新日志", "关于", "退出" };
|
String[] sm = { "添加", "同日搜索", "日期范围搜索", "分享数据库", "导出HTML", "导出CSV", "更新日志", "关于", "退出" };
|
||||||
for (int i=0; i<sm.length; i++) {
|
for (int i=0; i<sm.length; i++) {
|
||||||
menu.add(0, i, i, sm[i]);
|
menu.add(0, i, i, sm[i]);
|
||||||
}
|
}
|
||||||
|
@ -161,7 +162,7 @@ public class MainActivity extends Activity {
|
||||||
field.setAccessible(true);//设置该属性可以访问
|
field.setAccessible(true);//设置该属性可以访问
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
}
|
}
|
||||||
if (!slocation.equals("") && (!sevent.equals(""))) {
|
if (!slocation.isEmpty() && (!sevent.isEmpty())) {
|
||||||
DBHelper dbHelper = new DBHelper(getApplicationContext());
|
DBHelper dbHelper = new DBHelper(getApplicationContext());
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
//values.put("time", calendar.getTime().getTime());
|
//values.put("time", calendar.getTime().getTime());
|
||||||
|
@ -178,10 +179,10 @@ public class MainActivity extends Activity {
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (slocation.equals("")){
|
if (slocation.isEmpty()){
|
||||||
editText_location.setError("地点不能为空!");
|
editText_location.setError("地点不能为空!");
|
||||||
}
|
}
|
||||||
if (sevent.equals("")){
|
if (sevent.isEmpty()){
|
||||||
editText_event.setError("事件不能为空!");
|
editText_event.setError("事件不能为空!");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -205,6 +206,7 @@ public class MainActivity extends Activity {
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
assert field != null;
|
||||||
field.set(dialog, true);
|
field.set(dialog, true);
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
@ -214,6 +216,55 @@ public class MainActivity extends Activity {
|
||||||
builder.create().show();
|
builder.create().show();
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
|
builder = new AlertDialog.Builder(MainActivity.this);
|
||||||
|
builder.setIcon(android.R.drawable.ic_menu_search);
|
||||||
|
builder.setTitle("同日搜索");
|
||||||
|
DatePicker datePicker1 = new DatePicker(MainActivity.this);
|
||||||
|
datePicker1.setCalendarViewShown(false);
|
||||||
|
builder.setView(datePicker1);
|
||||||
|
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
int month = datePicker1.getMonth();
|
||||||
|
int day = datePicker1.getDayOfMonth();
|
||||||
|
|
||||||
|
//隐藏年:https://blog.csdn.net/csdnadcode/article/details/38760743,无效
|
||||||
|
// Field f[] = datePicker1.getClass().getDeclaredFields();
|
||||||
|
// for (Field field : f) {
|
||||||
|
// if (field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner")) {
|
||||||
|
// field.setAccessible(true);
|
||||||
|
// Object yearPicker = new Object();
|
||||||
|
// try {
|
||||||
|
// yearPicker = field.get(datePicker1);
|
||||||
|
// } catch (IllegalAccessException e) {
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// }
|
||||||
|
// ((View) yearPicker).setVisibility(View.GONE);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
//隐藏年:https://www.jianshu.com/p/425d6e46db53,无效
|
||||||
|
int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
|
||||||
|
if (daySpinnerId != 0) {
|
||||||
|
View daySpinner = datePicker1.findViewById(daySpinnerId);
|
||||||
|
if (daySpinner != null) {
|
||||||
|
daySpinner.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DBHelper helper = new DBHelper(MainActivity.this);
|
||||||
|
Cursor cursor = helper.queryMonthDay(month, day);
|
||||||
|
int count = cursor.getCount();
|
||||||
|
setTitle(month + 1 + "月" + day + "日的事件" + count);
|
||||||
|
adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item_event, cursor, from, to, 0);
|
||||||
|
adapter.setViewBinder(viewBinder);
|
||||||
|
listView.setAdapter(adapter);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
builder.setNegativeButton("取消", null);
|
||||||
|
builder.create().show();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
LayoutInflater layoutInflater1 = LayoutInflater.from(this);
|
LayoutInflater layoutInflater1 = LayoutInflater.from(this);
|
||||||
View view1 = layoutInflater1.inflate(R.layout.date_range, null);
|
View view1 = layoutInflater1.inflate(R.layout.date_range, null);
|
||||||
final DatePicker datePicker_start = (DatePicker) view1.findViewById(R.id.datePicker_start);
|
final DatePicker datePicker_start = (DatePicker) view1.findViewById(R.id.datePicker_start);
|
||||||
|
@ -236,7 +287,7 @@ public class MainActivity extends Activity {
|
||||||
}
|
}
|
||||||
if (date_end.getTime() >= date_start.getTime()) {
|
if (date_end.getTime() >= date_start.getTime()) {
|
||||||
DBHelper helper = new DBHelper(MainActivity.this);
|
DBHelper helper = new DBHelper(MainActivity.this);
|
||||||
Cursor cursor = helper.query(date_start.getTime(), date_end.getTime());
|
Cursor cursor = helper.queryRange(date_start.getTime(), date_end.getTime());
|
||||||
int count = cursor.getCount();
|
int count = cursor.getCount();
|
||||||
if (date_end.getTime() > date_start.getTime())
|
if (date_end.getTime() > date_start.getTime())
|
||||||
setTitle(SDF_date.format(date_start) + "到" + SDF_date.format(date_end) + "的事件" + count);
|
setTitle(SDF_date.format(date_start) + "到" + SDF_date.format(date_end) + "的事件" + count);
|
||||||
|
@ -282,7 +333,7 @@ public class MainActivity extends Activity {
|
||||||
});
|
});
|
||||||
builder1.create().show();
|
builder1.create().show();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 3:
|
||||||
File file = new File(DBHelper.DATABASE_NAME);
|
File file = new File(DBHelper.DATABASE_NAME);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
|
@ -294,7 +345,7 @@ public class MainActivity extends Activity {
|
||||||
Toast.makeText(getApplicationContext(), "数据库文件不存在", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getApplicationContext(), "数据库文件不存在", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 4:
|
||||||
String s = "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/>\n<title>事件</title>\n<style>a { text-decoration:none; }\ntable { table-layout:fixed; width:100%; border-collapse:collapse; }\nth, td { border:1px solid black; padding:5px; overflow:hidden; text-overflow: ellipsis; }\n</style>\n</head>\n<body>\n<h2 align=center>事件" + adapter.getCount() + "</h2>\n<table>\n<tr><th>时间</th><th>地点</th><th>事件</th></tr>\n";
|
String s = "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/>\n<title>事件</title>\n<style>a { text-decoration:none; }\ntable { table-layout:fixed; width:100%; border-collapse:collapse; }\nth, td { border:1px solid black; padding:5px; overflow:hidden; text-overflow: ellipsis; }\n</style>\n</head>\n<body>\n<h2 align=center>事件" + adapter.getCount() + "</h2>\n<table>\n<tr><th>时间</th><th>地点</th><th>事件</th></tr>\n";
|
||||||
for (int i=0; i<adapter.getCount(); i++){
|
for (int i=0; i<adapter.getCount(); i++){
|
||||||
LinearLayout layout = (LinearLayout) listView.getAdapter().getView(i, null, null);
|
LinearLayout layout = (LinearLayout) listView.getAdapter().getView(i, null, null);
|
||||||
|
@ -309,7 +360,7 @@ public class MainActivity extends Activity {
|
||||||
filename = editText_search.getText().toString() + ".htm";
|
filename = editText_search.getText().toString() + ".htm";
|
||||||
writeFile(filename, s);
|
writeFile(filename, s);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 5:
|
||||||
s = "";
|
s = "";
|
||||||
for (int i=0; i<adapter.getCount(); i++){
|
for (int i=0; i<adapter.getCount(); i++){
|
||||||
LinearLayout layout = (LinearLayout) listView.getAdapter().getView(i, null, null);
|
LinearLayout layout = (LinearLayout) listView.getAdapter().getView(i, null, null);
|
||||||
|
@ -323,19 +374,19 @@ public class MainActivity extends Activity {
|
||||||
filename = editText_search.getText().toString() + ".csv";
|
filename = editText_search.getText().toString() + ".csv";
|
||||||
writeFile(filename, s);
|
writeFile(filename, s);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 6:
|
||||||
new AlertDialog.Builder(this)
|
new AlertDialog.Builder(this)
|
||||||
.setIcon(R.mipmap.ic_launcher)
|
.setIcon(R.mipmap.ic_launcher)
|
||||||
.setTitle("更新日志")
|
.setTitle("更新日志")
|
||||||
.setMessage("V1.6 (2023-10)\n增加日期范围搜索。\n\nV1.5 (2023-02)\n移除DatePicker控件的startYear、endYear属性,去除日期限制。\n\nV1.4 (2022-11)\n修复:修改和删除后没按搜索词查询。\n\nV1.3 (2022-10)\n增加数据库文件分享。\n优化距今天数计算。\n\nV1.2 (2020-11)\n修复修改后没有按搜索词过滤。\n\nV1.1 (2020-08)\n增加跳转浏览器搜索。\n修改、删除记住位置。\n时间设置为24小时。\n\nV1.0 (2020-03)\n显示、添加、搜索日志,导出HTML、CSV。")
|
.setMessage("V1.7 (2024-08)\n增加同日搜索。\n\nV1.6 (2023-10)\n增加日期范围搜索。\n\nV1.5 (2023-02)\n移除DatePicker控件的startYear、endYear属性,去除日期限制。\n\nV1.4 (2022-11)\n修复:修改和删除后没按搜索词查询。\n\nV1.3 (2022-10)\n增加数据库文件分享。\n优化距今天数计算。\n\nV1.2 (2020-11)\n修复修改后没有按搜索词过滤。\n\nV1.1 (2020-08)\n增加跳转浏览器搜索。\n修改、删除记住位置。\n时间设置为24小时。\n\nV1.0 (2020-03)\n显示、添加、搜索日志,导出HTML、CSV。")
|
||||||
.setPositiveButton("确定", null).show();
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("事件日志 V1.6")
|
|
||||||
.setMessage("显示、添加、搜索日志,导出HTML、CSV。\n作者:海天鹰\nQQ:84429027")
|
|
||||||
.setPositiveButton("确定", null).show();
|
.setPositiveButton("确定", null).show();
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
|
new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("事件 V1.7")
|
||||||
|
.setMessage("显示、添加、搜索事件,导出HTML、CSV。\n作者:海天鹰\nQQ:84429027")
|
||||||
|
.setPositiveButton("确定", null).show();
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
finish();
|
finish();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue