日期范围搜索

1.日期范围选择界面

日期范围搜索_第1张图片




    

        

            

            

            

            

        

        

            

            

            

            

        

    

 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() {
     @ Override
    public 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中的私有属性mShowing
            field = 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);
            else
                setTitle(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() {
     @ Override
    public void onClick(DialogInterface dialog, int which) {
        Field field = null;
        try {
            //通过反射获取dialog中的私有属性mShowing
            field = 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");
    else
        c = db.query(TableName, null, "time >= " + date_start + " and time <= " + (date_start + 24 * 60 * 60 * 1000), null, null, null, "time asc");
    return c;
}

你可能感兴趣的:(Android,android)