2016-07-27 51 views
0

我插入所有數據並以圖形顯示,但如何僅顯示greendao最近7天的圖形?如何在greendao android中僅顯示7天記錄?

對於如:

  1. 如果我從今天開始插入數據。它應該只顯示今天的日期(比如說:例如:22/07)
  2. 然後第二天,它會插入數據。 (然後我需要顯示22/07,23/07)
  3. 如果我現在的日期是30/07,那麼我需要顯示(23/07,24/07/25/07到30/07)忽略其他數據即22/07。

但我得到所有的數據並顯示在我的情況。即從22/07到30/07。如何刪除舊的只顯示7天。 請幫我解決這個問題。

這是我的代碼,它顯示所有數據。

在DetectiveGraphExcecutor:

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 
} 

回答

0

如果我正確理解你的問題,我認爲它會更容易從數據庫中只得到所需要的數據。爲了做到這一點,你應該使用類似的東西,假設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();