我是android的新手。我使用内容提供商http://www.grokkingandroid.com/androids-calendarcontract-provider/创建了本地日历。现在我想在活动中显示今天的活动。我为此创建了一个游标。现在我想用这个光标显示事件,我非常有限。我需要帮助!
谢谢!
public void readCalendarEvents(Context context) {
Time time = new Time();
time.setToNow();
Long beginTime=time.toMillis(false);
time.set(59, 59, 23, time.monthDay, time.month, time.year);
Long endTime=time.toMillis(false);
String[] proj= new String[]{Instances._ID, Instances.TITLE, Instances.BEGIN, Instances.END, Instances.EVENT_ID};
Cursor cursor = Instances.query(context.getContentResolver(), proj, beginTime, endTime);
}
我修改我的代码以返回光标,片段将使用此光标显示日历事件列表。你能帮我验证这些代码吗?我在行上得到一个nullPointerException:cursor = myCalendar.readCalendarEvents(getActivity());
我的光标类:
public Class MyCalendar{
public Cursor readCalendarEvents(Context context) {
Time time = new Time();
time.setToNow();
Long beginTime = time.toMillis(false);
time.set(59, 59, 23, time.monthDay, time.month, time.year);
Long endTime = time.toMillis(false);
String[] proj = new String[] { Instances._ID, Instances.TITLE,
Instances.BEGIN, Instances.END, Instances.EVENT_ID };
Cursor cursor = Instances.query(context.getContentResolver(), proj,
beginTime, endTime);
return cursor;
} }
这是我的片段代码:
public class ViewMyCalendarEvents extends Fragment{
private MyCalendar myCalendar;
private Cursor cursor;
private ListView listView;
public ViewMyCalendarEvents() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(
R.layout.fragment_list_events, container, false);
listView = (ListView) view.findViewById(R.id.list_events_list);
cursor = myCalendar.readCalendarEvents(getActivity());
final String[] fromColumns = { "title", "beginDate", "endDate" };
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
R.layout.fragment_list_events_items, cursor, fromColumns,
new int[] { R.id.event_list_title, R.id.event_list_begin,
R.id.event_list_end }, 0);
listView.setAdapter(adapter);
return view;
}
}
谢谢!