Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

android - what are uri, contentValues

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...