如何查询特定日期范围的Android日历事件?
我在android中查询事件表以获取事件。
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);
这将返回所有事件。但现在我想要的事件只在特定的时间,说4/03/2013到7/03/2013。如何使用 selection
和 selectionArgs []
?
我试过这个
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};
但它不返回任何东西。我的怀疑是
1. where where子句工作?因为,startDate和endDate首先转换为long(毫秒),然后转换为String,然后存储在表中。因此,如何比较字符串的长值。在sqlite中没有 toNumber()
函数。
2.如果我通过 selectionArgs
然后 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 ?
startString
& endString
应以millis传递。尝试使用:
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)
此外,我认为你的逻辑是相反的,date范围应该是这样:
Also, I think your logic is reversed, date range should be like this:
String selection = "((dtstart >= "+c_start.getTimeInMillis()+") AND (dtend <= "+c_end.getTimeInMillis()+"))";