1.日期范围选择界面
<?xml version="1.0" encoding="utf-8"?>
<ScrollViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Spaceandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:ems="1"android:text="开始"android:textSize="30sp" /><DatePickerandroid:id="@+id/datePicker_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:calendarViewShown="false" /><Spaceandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Spaceandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:ems="1"android:text="结束"android:textSize="30sp" /><DatePickerandroid:id="@+id/datePicker_end"android:layout_width="wrap_content"android:layout_height="wrap_content"android:calendarViewShown="false" /><Spaceandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1" /></LinearLayout></LinearLayout>
</ScrollView>
2.加载界面,选择开始日期和结束日期,点击确定查询
String[] from = { "_id", "time", "location", "event" };
int[] to = { R.id.textView_id, R.id.textView_time, R.id.textView_location, R.id.textView_event };
SimpleCursorAdapter.ViewBinder viewBinder;viewBinder = new SimpleCursorAdapter.ViewBinder() {public boolean setViewValue(View view, Cursor cursor, int columnIndex) {if (view.getId() == R.id.textView_time) {Date date = new Date(cursor.getLong(columnIndex));((TextView)view).setText(SDF.format(date));return true;}return false;}
};
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.date_range, null);
final DatePicker datePicker_start = (DatePicker)view.findViewById(R.id.datePicker_start);
final DatePicker datePicker_end = (DatePicker)view.findViewById(R.id.datePicker_end);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_menu_search);
builder.setTitle("日期范围搜索");
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@ Overridepublic void onClick(DialogInterface dialog, int which) {Date date_start = new Date(datePicker_start.getYear() - 1900, datePicker_start.getMonth(), datePicker_start.getDayOfMonth());Date date_end = new Date(datePicker_end.getYear() - 1900, datePicker_end.getMonth(), datePicker_end.getDayOfMonth());Field field = null;try { //通过反射获取dialog中的私有属性mShowingfield = dialog.getClass().getSuperclass().getDeclaredField("mShowing");field.setAccessible(true); //设置该属性可以访问} catch (Exception e) {Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();}if (date_end.getTime() >= date_start.getTime()) {DBHelper helper = new DBHelper(MainActivity.this);Cursor cursor = helper.query(tableName, date_start.getTime(), date_end.getTime());int count = cursor.getCount();if (date_end.getTime() > date_start.getTime())setTitle(username + SDF_date.format(date_start) + "到" + SDF_date.format(date_end) + "的账目" + count);elsesetTitle(username + SDF_date.format(date_start) + "的账目" + count);adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item_cash, cursor, from, to, 0);adapter.setViewBinder(viewBinder);listView.setAdapter(adapter);try { //关闭field.set(dialog, true);dialog.dismiss();} catch (Exception e) {Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();}} else {Toast.makeText(getApplicationContext(), "结束日期比开始日期早!", Toast.LENGTH_SHORT).show();try { //设置dialog不可关闭field.set(dialog, false);dialog.dismiss();} catch (Exception e) {Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();}}}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@ Overridepublic void onClick(DialogInterface dialog, int which) {Field field = null;try {//通过反射获取dialog中的私有属性mShowingfield = dialog.getClass().getSuperclass().getDeclaredField("mShowing");field.setAccessible(true); //设置该属性可以访问} catch (Exception ex) {}try {field.set(dialog, true);dialog.dismiss();} catch (Exception e) {Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();}}
});
builder.create().show();
3.查询
public Cursor query(long date_start, long date_end) {db = getWritableDatabase();Cursor c;if (date_start != date_end)c = db.query(TableName, null, "time >= " + date_start + " and time <= " + (date_end + 24 * 60 * 60 * 1000), null, null, null, "time asc");elsec = db.query(TableName, null, "time >= " + date_start + " and time <= " + (date_start + 24 * 60 * 60 * 1000), null, null, null, "time asc");return c;
}