gpt4 book ai didi

java - 如何从我的 Android 应用程序中删除或编辑 G-calendar 中的事件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:20 26 4
gpt4 key购买 nike

我正在获取 Google 日历 Activity 列表。

我可以获得哪个事件标识符能够删除和编辑特定事件

这是我的获取代码:

private void fetchEvents() {
String[] selection = new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" };

String projection = "description LIKE ?";
String[] selecionArgs = new String[]{"%/images/%"};
String orderby = "dtstart ASC";

Cursor cursor = getContentResolver()
.query(
Uri.parse("content://com.android.calendar/events"),
selection, projection,
selecionArgs, orderby);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];

for (int i = 0; i < CNames.length; i++) {
nameOfEvent.add(cursor.getString(1));
cursor.moveToNext();
}
}

这里我想删除一个事件(请问我应该使用哪种类型进行编辑?)

    private void deleteCalendarEvent_intent() {
Intent intent = new Intent(Intent.ACTION_DELETE)

//?
startActivity(intent);
}

最佳答案

为了能够删除某些事件,您在获取事件列表时需要它的 _id。您可以通过将 "_id" 添加到您的选择数组来获得它。删除方法可能如下所示

private void deleteEvent(int eventId)
{
Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/events");
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, eventId);
getContentResolver().delete(uri, null, null);
}

重要:你应该知道,调用这个并不会真正删除事件数据库记录,而只是标记要删除的记录,并将设置列 deleted 设置为 1。这是因为同步适配器启用同步在服务器上删除。

知道这一点后,您应该编辑未删除的事件查询以不返回已删除的事件。示例如下:

private List<CalendarEntry> readCalendar()
{
// Fetch a list of all calendars synced with the device, their title and _id
// Notice that there is selection deleted = 0.
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"),
(new String[]{"_id", "title"}), "deleted = ?", new String[]{"0"}, null);

List<CalendarEntry> calendarIds = new ArrayList<CalendarEntry>();

while (cursor.moveToNext())
{
int _id = cursor.getInt(0);
String title = cursor.getString(1);
calendarIds.add(new CalendarEntry(_id, title));
}
cursor.close();
return calendarIds;
}

您只需浏览列表并选择要删除的事件即可。

这是列表的完整工作示例,其中包含点击项目时的删除事件。

public class DeleteCalendarEventsActivity extends ListActivity
{
private TestAdapter mTestAdapter;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.list);

mTestAdapter = new TestAdapter(this);
setListAdapter(mTestAdapter);

refreshList();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
int eventId = mTestAdapter.getItem(position).mId;
deleteEvent(eventId);
refreshList();
}

private void deleteEvent(int eventId)
{
Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/events");
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, eventId);
getContentResolver().delete(uri, null, null);
}

private void refreshList()
{
mTestAdapter.clear();
for (CalendarEntry calendarEntry : readCalendar())
{
mTestAdapter.add(calendarEntry);
}
}

private List<CalendarEntry> readCalendar()
{
// Fetch a list of all calendars synced with the device, their title and _id
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"),
(new String[]{"_id", "title"}), "deleted = ?", new String[]{"0"}, null);

List<CalendarEntry> calendarIds = new ArrayList<CalendarEntry>();

while (cursor.moveToNext())
{
int _id = cursor.getInt(0);
String title = cursor.getString(1);
calendarIds.add(new CalendarEntry(_id, title));
}
cursor.close();
return calendarIds;
}

static class TestAdapter extends ArrayAdapter<CalendarEntry>
{
TestAdapter(Context context)
{
super(context, 0);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, null);
}

TextView textView = (TextView) convertView;
textView.setText(getItem(position).mTitle);

return convertView;
}
}

static class CalendarEntry
{
private final int mId;
private final String mTitle;

CalendarEntry(int id, String title)
{
mId = id;
mTitle = title;
}
}
}

关于java - 如何从我的 Android 应用程序中删除或编辑 G-calendar 中的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20592260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com