一个Android的投影图的内容提供者的目的是什么?投影图、目的、提供者、内容

由网友(後世、續前緣)分享简介:我在看Android的记事本应用程序示例code。在< path_to_SDK> /样本/ Android为16 /记事本/ src目录/ COM /例子/安卓/记事本。I am looking at the Android notepad application sample code in 我在看Android的记事本应用程序示例code。在< path_to_SDK> /样本/ Android为16 /记事本/ src目录/ COM /例子/安卓/记事本

I am looking at the Android notepad application sample code in <path_to_SDK>/samples/android-16/NotePad/src/com/example/android/notepad.

我想知道如果任何人都可以向我解释为什么以下code是需要在 NotepadProvider.java

I was wondering if anyone could explain to me why the following code is needed in NotepadProvider.java?

// Creates a new projection map instance. The map returns a column name
// given a string. The two are usually equal.
sNotesProjectionMap = new HashMap<String, String>();

// Maps the string "_ID" to the column name "_ID"
sNotesProjectionMap.put(NotePad.Notes._ID, NotePad.Notes._ID);

// Maps "title" to "title"
sNotesProjectionMap.put(NotePad.Notes.COLUMN_NAME_TITLE,NotePad.Notes.COLUMN_NAME_TITLE);

// Maps "note" to "note"
sNotesProjectionMap.put(NotePad.Notes.COLUMN_NAME_NOTE, NotePad.Notes.COLUMN_NAME_NOTE);

// Maps "created" to "created"
sNotesProjectionMap.put(NotePad.Notes.COLUMN_NAME_CREATE_DATE, NotePad.Notes.COLUMN_NAME_CREATE_DATE);

// Maps "modified" to "modified"
sNotesProjectionMap.put(
        NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,
        NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE)

我注意到投影映射在查询以后使用()方法:

...
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(NotePad.Notes.TABLE_NAME);

/**
 * Choose the projection and adjust the "where" clause based on URI pattern-matching.
 */
switch (sUriMatcher.match(uri)) {
    // If the incoming URI is for notes, chooses the Notes projection
    case NOTES:
    qb.setProjectionMap(sNotesProjectionMap);
    break;
...

这是为什么投影图需要?

Why is this projection map needed?

推荐答案

SDK 演示的记事本应用是一个示例应用程序,一个应该是API的使用,并使用这些API的良好做法的例子,这就是为什么他们可能使用一个投影图。虽然记事本样品并不真的需要一个投影图使用一个是对于更复杂的情况下获得良好的展示时,为需要的人。举例来说,如果我记得没错,是利用投影图在其的ContentProvider 和投影映射不仅仅是具有相同键 - 值对一个简单的映射。

The Notepad application from the SDK demos is a sample application, one that should be an example of API use and good practices using those APIs, that is why they probably use a projection map. Although the Notepad sample doesn't really need a projection map the use of one is a good showcase for more complex cases when one is needed. For example, if I remember right, the Shelves application written by one of the Google engineers is using a projection map in its ContentProvider and that projection map isn't just a simple mapping with identical key-value pairs.

我还添加了一个链接到方法的文档 SQLiteQueryBuilder.setProjectionMap 这对你为什么会需要一个投影图的一些细节。

I've also added a link to the documentation of the method SQLiteQueryBuilder.setProjectionMap which has some details on why you would need a projection map.