因為項目需要互订,需要按時間進行查詢數(shù)據(jù)
圖片發(fā)自簡書App
這里要是用數(shù)據(jù)庫select*from進行條件判斷
點擊Edittext輸入時間格式y(tǒng)yyy-mm-dd
SQLiteDatabase sqliteDatabase=helper.getReadableDatabase();
String ss="select*from heart where heartdata>? and heartdata<?"; //查詢條件
Date s=getDate(startEdittext.getText().toString());
Date e=getDate(endEdittext.getText().toString());
Calendar cal = Calendar.getInstance();
cal.setTime(s);
Long startTime=cal.getTimeInMillis();
Calendar cals = Calendar.getInstance();
cals.setTime(e);
Long endTime=cals.getTimeInMillis();
String first="1970-01-03";
Date j=getDate(first);
Calendar cass=Calendar.getInstance();
cass.setTime(j);
Long firsTtime=cass.getTimeInMillis();
String second="1970-01-02";
Date h=getDate(second);
Calendar calh=Calendar.getInstance();
calh.setTime(h);
Long secondTime=calh.getTimeInMillis();
//Long thirdTime=firsTtime-secondTime;
Long thirdTime=endTime+(firsTtime-secondTime);
Toast.makeText(SearchActivity.this,thirdTime+"-",Toast.LENGTH_LONG).show();
Cursor cursor = sqliteDatabase.rawQuery(ss,new String[] { String.valueOf(startTime), String.valueOf(thirdTime) });
if(cursor.moveToLast()){
do{
Long hd=cursor.getLong(cursor.getColumnIndex("heartdata"));
String heart_num=cursor.getString(cursor.getColumnIndex("heartnum"));
SimpleDateFormat dq=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
String hdd=dq.format(hd);
traceList.add(new Trace(hdd,heart_num));
//Toast.makeText(SearchActivity.this,hdd,Toast.LENGTH_SHORT).show();
}while(cursor.moveToPrevious());
aapter = new TraceListAdapter(this, traceList);
lv.setAdapter(aapter);
aapter.notifyDataSetChanged();
cursor.close();
}
}
代碼中的getDate()函數(shù)子库,是將字符串轉化為日期的函數(shù)温数,
將日期轉化為毫秒,例如一天轉化為毫秒是24×3600×1000
起始時間是從“1970-01-01 08:00:00”開始算起的,所以我要得到一天的毫秒數(shù),就要用1970-01-03減去1970-01-02
我的代碼不夠精簡结闸,大致思路就是這樣。
圖片發(fā)自簡書App