• 日期范围搜索


    1.日期范围选择界面

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <ScrollView
    3. android:layout_width="fill_parent"
    4. android:layout_height="fill_parent"
    5. xmlns:android="http://schemas.android.com/apk/res/android">
    6. <LinearLayout
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content"
    9. android:orientation="vertical">
    10. <LinearLayout
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content">
    13. <Space
    14. android:layout_width="wrap_content"
    15. android:layout_height="wrap_content"
    16. android:layout_weight="1" />
    17. <TextView
    18. android:layout_width="wrap_content"
    19. android:layout_height="wrap_content"
    20. android:layout_gravity="center"
    21. android:ems="1"
    22. android:text="开始"
    23. android:textSize="30sp" />
    24. <DatePicker
    25. android:id="@+id/datePicker_start"
    26. android:layout_width="wrap_content"
    27. android:layout_height="wrap_content"
    28. android:calendarViewShown="false" />
    29. <Space
    30. android:layout_width="wrap_content"
    31. android:layout_height="wrap_content"
    32. android:layout_weight="1" />
    33. </LinearLayout>
    34. <LinearLayout
    35. android:layout_width="match_parent"
    36. android:layout_height="wrap_content">
    37. <Space
    38. android:layout_width="wrap_content"
    39. android:layout_height="wrap_content"
    40. android:layout_weight="1" />
    41. <TextView
    42. android:layout_width="wrap_content"
    43. android:layout_height="wrap_content"
    44. android:layout_gravity="center"
    45. android:ems="1"
    46. android:text="结束"
    47. android:textSize="30sp" />
    48. <DatePicker
    49. android:id="@+id/datePicker_end"
    50. android:layout_width="wrap_content"
    51. android:layout_height="wrap_content"
    52. android:calendarViewShown="false" />
    53. <Space
    54. android:layout_width="wrap_content"
    55. android:layout_height="wrap_content"
    56. android:layout_weight="1" />
    57. </LinearLayout>
    58. </LinearLayout>
    59. </ScrollView>

     2.加载界面,选择开始日期和结束日期,点击确定查询

    1. String[] from = { "_id", "time", "location", "event" };
    2. int[] to = { R.id.textView_id, R.id.textView_time, R.id.textView_location, R.id.textView_event };
    3. SimpleCursorAdapter.ViewBinder viewBinder;
    4. viewBinder = new SimpleCursorAdapter.ViewBinder() {
    5. public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
    6. if (view.getId() == R.id.textView_time) {
    7. Date date = new Date(cursor.getLong(columnIndex));
    8. ((TextView)view).setText(SDF.format(date));
    9. return true;
    10. }
    11. return false;
    12. }
    13. };
    1. LayoutInflater layoutInflater = LayoutInflater.from(this);
    2. View view = layoutInflater.inflate(R.layout.date_range, null);
    3. final DatePicker datePicker_start = (DatePicker)view.findViewById(R.id.datePicker_start);
    4. final DatePicker datePicker_end = (DatePicker)view.findViewById(R.id.datePicker_end);
    5. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    6. builder.setIcon(android.R.drawable.ic_menu_search);
    7. builder.setTitle("日期范围搜索");
    8. builder.setView(view);
    9. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    10. @ Override
    11. public void onClick(DialogInterface dialog, int which) {
    12. Date date_start = new Date(datePicker_start.getYear() - 1900, datePicker_start.getMonth(), datePicker_start.getDayOfMonth());
    13. Date date_end = new Date(datePicker_end.getYear() - 1900, datePicker_end.getMonth(), datePicker_end.getDayOfMonth());
    14. Field field = null;
    15. try { //通过反射获取dialog中的私有属性mShowing
    16. field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
    17. field.setAccessible(true); //设置该属性可以访问
    18. } catch (Exception e) {
    19. Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    20. }
    21. if (date_end.getTime() >= date_start.getTime()) {
    22. DBHelper helper = new DBHelper(MainActivity.this);
    23. Cursor cursor = helper.query(tableName, date_start.getTime(), date_end.getTime());
    24. int count = cursor.getCount();
    25. if (date_end.getTime() > date_start.getTime())
    26. setTitle(username + SDF_date.format(date_start) + "到" + SDF_date.format(date_end) + "的账目" + count);
    27. else
    28. setTitle(username + SDF_date.format(date_start) + "的账目" + count);
    29. adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item_cash, cursor, from, to, 0);
    30. adapter.setViewBinder(viewBinder);
    31. listView.setAdapter(adapter);
    32. try { //关闭
    33. field.set(dialog, true);
    34. dialog.dismiss();
    35. } catch (Exception e) {
    36. Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    37. }
    38. } else {
    39. Toast.makeText(getApplicationContext(), "结束日期比开始日期早!", Toast.LENGTH_SHORT).show();
    40. try { //设置dialog不可关闭
    41. field.set(dialog, false);
    42. dialog.dismiss();
    43. } catch (Exception e) {
    44. Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    45. }
    46. }
    47. }
    48. });
    49. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    50. @ Override
    51. public void onClick(DialogInterface dialog, int which) {
    52. Field field = null;
    53. try {
    54. //通过反射获取dialog中的私有属性mShowing
    55. field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
    56. field.setAccessible(true); //设置该属性可以访问
    57. } catch (Exception ex) {}
    58. try {
    59. field.set(dialog, true);
    60. dialog.dismiss();
    61. } catch (Exception e) {
    62. Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    63. }
    64. }
    65. });
    66. builder.create().show();

    3.查询

    1. public Cursor query(long date_start, long date_end) {
    2. db = getWritableDatabase();
    3. Cursor c;
    4. if (date_start != date_end)
    5. c = db.query(TableName, null, "time >= " + date_start + " and time <= " + (date_end + 24 * 60 * 60 * 1000), null, null, null, "time asc");
    6. else
    7. c = db.query(TableName, null, "time >= " + date_start + " and time <= " + (date_start + 24 * 60 * 60 * 1000), null, null, null, "time asc");
    8. return c;
    9. }

  • 相关阅读:
    方法覆盖与方法重载
    史诗级PCL和Eigen联合BUG
    分布式存储系统Ceph应用组件介绍
    初识Apifox——如何使用Apifox做一个简单的接口测试
    https加密协议全过程(结合全站文章,最简单直观的小白文)
    MySQL---查询(下篇)
    高企!2022年武汉市高新技术企业奖励补贴以及申报条件汇总!
    后端面经学习自测(二)
    jmeter5.4.1源码编译(IDEA)问题解决
    好奇喵 | PT(Private Tracker)——什么是P2P,什么是BT,啥子是PT?
  • 原文地址:https://blog.csdn.net/sonichty/article/details/133466326