Android 2.3的android日历事件问题

时间:2012-12-12 07:07:48

标签: android android-calendar

我使用下面提到的代码向用户显示添加日历事件屏幕。

例如,以下内容将提示用户是否应创建具有某些详细信息的事件。

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent);

这部分适用于Android 4.0及以上版本,但不适用于Android 2.3 .... 我希望这适用于2.3到4.1之间的所有Android操作系统。

2 个答案:

答案 0 :(得分:0)

如果您使用其他方式,也可以使用它:

mCursor = getContentResolver().query(
CalendarContract.Events.CONTENT_URI, COLS, null, null, null);

它是一个用于日历的contentProvider。

答案 1 :(得分:0)

public class Main extends Activity implements OnClickListener{
private Cursor mCursor = null;
private static final String[] COLS = new String[]
{ CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART};
}

现在我们需要覆盖on create方法。特别注意我们如何填充数据库游标。这是我们需要先前定义的COLS常数的地方。您还将注意到,在初始化数据库游标并设置了单击处理程序回调之后,我们继续并手动调用on click处理程序。此快捷方式允许我们最初填写我们的UI而无需重复代码。

Main.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCursor = getContentResolver().query(
CalendarContract.Events.CONTENT_URI, COLS, null, null, null);
mCursor.moveToFirst();
Button b = (Button)findViewById(R.id.next);
b.setOnClickListener(this);
b = (Button)findViewById(R.id.previous);
b.setOnClickListener(this);
onClick(findViewById(R.id.previous));
}

在我们的回调中,我们将把光标操作到数据库中的正确条目并更新UI。

@Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.data);
String title = "N/A";
Long start = 0L;
switch(v.getId()) {
case R.id.next:
if(!mCursor.isLast()) mCursor.moveToNext();
break;
case R.id.previous:
if(!mCursor.isFirst()) mCursor.moveToPrevious();
break;
}
Format df = DateFormat.getDateFormat(this);
Format tf = DateFormat.getTimeFormat(this);
try {
title = mCursor.getString(0);
start = mCursor.getLong(1);
} catch (Exception e) {
//ignore
}
tv.setText(title+" on "+df.format(start)+" at "+tf.format(start));
}