How to query android calendar events for specific date range?

佐手、 提交于 2019-12-08 16:41:36

问题


I am querying events table in android to get events.

String[] projection = new String[] { "calendar_id", "title", "description",
                    "dtstart", "dtend", "eventLocation" };

Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/events"),
                    projection,
                    null,
                    null,
                    null);

This returns all the events. But now I want events only in specific duration say 4/03/2013 to 7/03/2013. How do I use selection and selectionArgs[] ? I tried this

String selection = "((dtstart <= ?) AND (dtend >= ?))";
String[] selectionArgs = new String[] {startString, endString};

But its not returning anything. My doubt are 1. Will where clause work ? Because, startDate and endDate are first converted to long (milliseconds) and then to String and then are stored in the table. So how can strings be compared for their long values. There is no toNumber() function in sqlite. 2. If I'm passing selectionArgs then startString and endString should be in which format ?


回答1:


startString & endString should be passed in millis. Try using:

Calendar c_start= Calendar.getInstance();
c_start.set(2013,2,4,0,0); //Note that months start from 0 (January)   
Calendar c_end= Calendar.getInstance();
c_end.set(2013,2,7,0,0); //Note that months start from 0 (January)

Also, I think your logic is reversed, date range should be like this:

String selection = "((dtstart >= "+c_start.getTimeInMillis()+") AND (dtend <= "+c_end.getTimeInMillis()+"))";


来源:https://stackoverflow.com/questions/15477859/how-to-query-android-calendar-events-for-specific-date-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!