gpt4 book ai didi

android - 什么是 uri,contentValues

转载 作者:IT老高 更新时间:2023-10-28 21:51:18 25 4
gpt4 key购买 nike

谁能解释一下我在处理日历事件时使用的每个术语?

  1. Uri event_uri = Uri.parse("content://com.android.calendar/" + "events");
    这里的 uri 是什么,实际上是什么内容,因为我们可以将 int 值初始化为 0?是吗
    可以用默认值初始化 uri 吗?

  2. Uri reminder_uri = Uri.parse("content://com.android.calendar/" + "reminders");
    这些uri是什么意思? event_urireminder_uri有什么区别?

  3. ContentValues values = new ContentValues();<br/>
    values.put("calendar_id", 1);<br/>
    values.put("title", str);<br/>
    values.put("description", m_strDescription);

    第一个是做什么的? values.put("calendar_id", 1);

  4. ContentResolver cr = getContentResolver();
    内容解析器有什么用?有时我们会写:

    Uri u = cr.insert(event_uri, values)
    这是什么uri?它与前两个 uri 有何不同,例如 event_urireminder_uri

    又是 values.put("event_id", Long.parseLong(event.getLastPathSegment()));
    cr.insert(remindar_uri, values);

    它有什么作用?

最佳答案

关于问题 1 和 2,Uri 是指向重要事物的地址。在 ContentProvider 的情况下,Uri 通常用于确定使用哪个表。所以 event_uri 指向事件表,reminder_uri 指向提醒表。 uris 确实没有“默认值”。

关于问题3,ContentValues本质上是一组键值对,其中键代表表的列,值是要插入该列的值。所以在 values.put("calendar_id", 1); 的情况下,列是“calendar_id”,为该列插入的值是 1。

关于问题4,ContentResolver是android用来将Uris解析为ContentProviders的。任何人都可以创建 ContentProvider,Android 有用于日历、联系人等的 ContentProvider 上的 insert() 方法ContentResolver 返回插入行的 Uri。所以在问题 1 和 2 中,那些 Uri 指向表,但 Uri 是分层的,因此它们可以解析到特定行。例如:

content://com.android.calendar/events 指向事件表,但是

content://com.android.calendar/events/1 指向事件表中 id 为 1 的行。

请记住,这是通常的行为,但提供 ContentProvider 可以自定义 uri 以不同方式解析。

我强烈推荐阅读 ContentProvider docs ,尤其是关于 Content URIs 的部分.


来自之前推荐的文档:

In the previous lines of code, the full URI for the "words" table is:

content://user_dictionary/words

where the user_dictionary string is the provider's authority, and words string is the table's path. The string content:// (the scheme) is always present, and identifies this as a content URI.

关于android - 什么是 uri,contentValues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7426451/

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