什么是 uri、contentValues

发布于 2024-12-04 10:30:27 字数 979 浏览 0 评论 0原文

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

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

  2. Uri Remember_uri = Uri.parse("content://com.android.calendar/" + "提醒");
    这些uri代表什么? event_urireminder_uri 之间有什么区别?

  3. ContentValues 值 = new ContentValues();
    value.put("calendar_id", 1);
    value.put("标题", str);
    value.put("描述", m_strDescription);

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

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

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

    再次values.put("event_id", Long.parseLong(event.getLastPathSegment())); cr.insert(remindar_uri, 值);

    它有什么作用?

Can anyone explain me about each term that I have used in working with calendar events?

  1. Uri event_uri = Uri.parse("content://com.android.calendar/" + "events");
    What is uri here, what actually is content, as we can initialize int value to 0? Is it
    possible to initialize a uri with a default value?

  2. Uri reminder_uri = Uri.parse("content://com.android.calendar/" + "reminders");
    What signifies these uri? What are the differences between event_uri and reminder_uri?

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

    What does the first one do? values.put("calendar_id", 1);

  4. ContentResolver cr = getContentResolver();
    What is the use of the content resolver? Sometimes we write:

    Uri u = cr.insert(event_uri, values)
    What is this uri? How does it differ from the first two uris e.g event_uri and reminder_uri

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

    What does it do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月夜 2024-12-11 10:30:27

关于问题 1 和 2,Uri 是一个指向重要内容的地址。对于 ContentProviderUri 通常用于确定要使用哪个表。因此,event_uri 指向事件表,reminder_uri 指向提醒表。 uris 实际上没有“默认值”。

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

关于问题 4,ContentResolver 是 android 用于将 Uri 解析为 ContentProvider 的方法。任何人都可以创建 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 文档,特别是关于 内容 URI


从之前推荐的文档来看:

在前面的代码行中,“words”表的完整 URI 为:

content://user_dictionary/words

user_dictionary 字符串在哪里
提供者的权限,words字符串是表的路径。这
字符串 content://scheme)始终存在,并标识此
作为内容 URI。

Regarding questions 1 and 2, A Uri is an address that points to something of significance. In the case of ContentProviders, the Uri is usually used to determine which table to use. So event_uri points to the events table and the reminder_uri points to the reminders table. There is really no "default value" for uris.

Regarding question 3, the ContentValues is essentially a set of key-value pairs, where the key represents the column for the table and the value is the value to be inserted in that column. So in the case of values.put("calendar_id", 1);, the column is "calendar_id" and the value being inserted for that column is 1.

Regarding question 4, the ContentResolver is what android uses to resolve Uris to ContentProviders. Anyone can create a ContentProvider and Android has ContentProviders for the Calendar, Contacts, etc.. The insert() method on a ContentResolver returns the Uri of the inserted row. So in questions 1 and 2, those Uris pointed to the table but Uris are hierarchical so they can resolve to a specific row. For example:

content://com.android.calendar/events points to the events table, but

content://com.android.calendar/events/1 points to the row in the events table with id 1.

Keep in mind, that this is the usual behavior, but the providing ContentProvider can customize the uris to be resolved differently.

I would strongly recommend reading the ContentProvider docs, especially the section on Content URIs.


From the previously recommended documentation:

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.

请止步禁区 2024-12-11 10:30:27

内容值:

public final class
ContentValues
extends Object
implements Parcelable have public methods
like: String getAsString(String KEY) , void put(Sring KEY, String value)
e.g.

public void createEntry(String name, String number){
    ContentValues cval = new ContentValues();
    cval.put(KEY_NAME, name);   // KEY_NAME is key just like parameter in put(String key)
    ....
    ....
    ....


}

ContentValues:

public final class
ContentValues
extends Object
implements Parcelable have public methods
like: String getAsString(String KEY) , void put(Sring KEY, String value)
e.g.

public void createEntry(String name, String number){
    ContentValues cval = new ContentValues();
    cval.put(KEY_NAME, name);   // KEY_NAME is key just like parameter in put(String key)
    ....
    ....
    ....


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文