如何查询特定日期范围的Android日历事件? [英] How to query android calendar events for specific date range?

查看:267
本文介绍了如何查询特定日期范围的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()+"))";

这篇关于如何查询特定日期范围的Android日历事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆