我正在插入所有数据并在图中显示,但如何只显示greendao的最后7天图?
例如:
- 如果我今天开始插入数据。它应该只显示今天的日期。(例如:2007年22日)
- 然后第二天,它将插入数据。(然后我需要显示22/07、23/07)
- 如果我的当前日期是2007年7月30日,那么我需要显示(07年7月23日、07年07月24日至07年30日),忽略其他数据,即07年22日
但我正在获取所有数据并在我的案例中显示。即从2007年22月到2007年30月。如何通过删除旧的7天来显示。请帮我解决这个问题。
这是我的代码,它显示所有数据。
在DetectiveGraphExcector:中
public List<DetectiveGraph> getDetectionsGraphBetweenTime(Date fromDateAndTime, Date toTimeAndDate) {
List<DetectiveGraph> mTask = mDetectiveGraphDao.queryBuilder().where(DetectiveGraphDao.Properties.Detective_date.between(fromDateAndTime, toTimeAndDate)).list();
closeSession();
return mTask;
}
public List<DetectiveGraph> getAllDetections() { //getting all datas
return mDetectiveGraphDao.queryBuilder().orderAsc(DetectiveGraphDao.Properties.Detective_date).list();
}
public class ActivityTracking extends Activity{
private DetectiveGraphExecutor mDetectiveGraphExecutor;
private List<DetectiveGraph> mDetectiveGraphList;
private int columnIndex;
private List<DetectiveGraph> activities; //The activities.
@Override
protected void onCreate(Bundle savedInstanceState) { //onCreate
super.onCreate(savedInstanceState); //super
setContentView(R.layout.activity_tracking);
mDetectiveGraphList = mDetectiveGraphExecutor.getAllDetections();
maxDate = mDetectiveGraphList.get(mDetectiveGraphList.size()-1).getDetectiveDate();
toDates = maxDate; // maximumDate
Calendar toDateCalendar = Calendar.getInstance(); //toDateCalendar
toDateCalendar.setTime(toDates); //toDateCalendar
Calendar fromDateCalendar = (Calendar) toDateCalendar.clone();
fromDateCalendar.add(Calendar.DATE, -7); //fromDateCalendar
fromDateCalendar.add(Calendar.MINUTE, -fromDateCalendar.get(Calendar.MINUTE)); //minute
fromDateCalendar.set(Calendar.SECOND, 0); //second
Long initialLogTime = Long.valueOf(VALRTApplication.getPrefString(ActivityTracking.this, "epochTime"));
Calendar calendar = Calendar.getInstance(); //calendar
calendar.setTimeInMillis(initialLogTime); //setTime
calendar.add(Calendar.MINUTE, -calendar.get(Calendar.MINUTE)); //minute
calendar.set(Calendar.SECOND, 0); //second
Calendar fromDateTime = calendar; //fromDate
columnIndex = -1; //columnIndex
do {
Date fromDate = null, toDate = null; //fromDate
Calendar toDateTime = (Calendar) fromDateTime.clone(); //toDate
toDateTime.add(Calendar.MINUTE, 60); //toDateTime
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss:aa"); //dateFormat
dateString = dateFormat.format(fromDateTime.getTime()); //dateString
dateString1 = dateFormat.format(toDateTime.getTime()); //dateString1
try {
fromDate = dateFormat.parse(dateString); //dateString
toDate = dateFormat.parse(dateString1);//toDate
} catch (ParseException e) {
LogUtils.e(VALRTApplication.TAG, e); //exceptional handling
}
if (fromDate != null && toDate != null) { //fromDate and to date not null
updateGraph(fromDate, toDate, entries, labels);
}
textName.setText(iAddedDate); //setText
fromDateTime.add(Calendar.HOUR, 1); //add
}
while (Calendar.getInstance().getTimeInMillis() >= fromDateTime.getTimeInMillis()); //getTime
}
//in this I am updating graph based on the date and updating in UI.
private void updateGraph(Date fromDate, Date toDate, List<BarEntry> entries, ArrayList<String> labels) {
activities = mDetectiveGraphExecutor.getDetectionsGraphBetweenTime(fromDate, toDate); //getDetectionGraph
SimpleDateFormat dateFormats = new SimpleDateFormat("h:aa"); //hour and am/pm
SimpleDateFormat withDateTimes = new SimpleDateFormat("MM/dd/yyyy"); //withDateTimes
iAddedDate = withDateTimes.format(toDate.getTime()); //getTime
String newDate = iAddedDate.substring(0, iAddedDate.length() - 5); //newDate
String dateToTimes = dateFormats.format(toDate.getTime()); //getTime
String replaceTime = dateToTimes.replaceAll(":", " ").toUpperCase(); //toUpperCase
String outputDates = "n" + newDate + "n" + replaceTime; //replaceTime
entries.add(new BarEntry(activities.size(), ++columnIndex)); //columnIndex
labels.add(outputDates); //outputDates
BarDataSet dataset = new BarDataSet(entries, ""); //dataset
BarData data = new BarData(labels, dataset); //data
data.setDrawValues(false); //setDrawValues
barChart.setData(data); //setData
}
如果我正确理解您的问题,我认为从数据库中只获取所需的数据会更容易。为了做到这一点,您应该使用类似的方法,假设Detective_date
是一个日期类型的列。
Calendar toDateCalendar = Calendar.getInstance();
Calendar fromDateCalendar = Calendar.getInstance()
fromDateCalendar.add(Calendar.DATE, -7);
List<DetectiveGraph> filteredDetections = mDetectiveGraphDao.queryBuilder()
.where(DetectiveGraphDao.Properties.Detective_date.ge(fromDateCalendar.getTime()),
DetectiveGraphDao.Properties.Detective_date.le(toDateCalendar.getTime()))
.orderAsc(DetectiveGraphDao.Properties.Detective_date)
.list();