【发布时间】:2014-05-24 14:50:38
【问题描述】:
更新:看着"vnd.android.cursor.dir/vnd.google.note" 和"vnd.android.cursor.item/vnd.google.note",在我看来,光标好像是针对一张桌子的。
从示例中可以看出,内容提供程序似乎设计为与一张表一起使用。我确实知道如何在 sqlite 中使用多个表,但在我看来,内容提供者似乎是要从一个表中选择一行或多行。
见http://developer.android.com/guide/topics/providers/content-provider-creating.html
另外,请参阅 adt-bundle-windows-x86-20131030\sdk\samples\android-19\legacy\NotePad\src\com\example\android\notepad 中的记事本示例
假设我想按主题做笔记。
我想要一个包含 _id 和 Title_text 列的主题表。 我想要带有 _id 列和外键 Topic_id 和 Note_text 的 Notes 表。
如何设计主题和注释?
但是查看 Notes 示例、内容提供程序上的内容 URI 和文档,似乎拥有多个相关表是事后才想到的,对我来说并不明显。
来自 NotepadProvider.java、Notepad.java:
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.note";
/**
* The MIME type of a {@link #CONTENT_URI} sub-directory of a single
* note.
*/
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.note";
public static final Uri CONTENT_ID_URI_BASE
= Uri.parse(SCHEME + AUTHORITY + PATH_NOTE_ID);
/**
* The content URI match pattern for a single note, specified by its ID. Use this to match
* incoming URIs or to construct an Intent.
*/
public static final Uri CONTENT_ID_URI_PATTERN
= Uri.parse(SCHEME + AUTHORITY + PATH_NOTE_ID + "/#");
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
...
switch (sUriMatcher.match(uri)) {
// If the incoming URI is for notes, chooses the Notes projection
case NOTES:
qb.setProjectionMap(sNotesProjectionMap);
break;
/* If the incoming URI is for a single note identified by its ID, chooses the
* note ID projection, and appends "_ID = <noteID>" to the where clause, so that
* it selects that single note
*/
case NOTE_ID:
qb.setProjectionMap(sNotesProjectionMap);
qb.appendWhere(
NotePad.Notes._ID + // the name of the ID column
"=" +
// the position of the note ID itself in the incoming URI
uri.getPathSegments().get(NotePad.Notes.NOTE_ID_PATH_POSITION));
break;
【问题讨论】:
-
所以你不知道如何在sqlite中使用多个表?
-
@pskink 我当然知道如何在 sqlite 中使用多个表。内容提供者似乎是要从一张表中选择一行或多行。
-
为什么是一张桌子?只返回一个从多个表中选择行的游标,这里有什么问题?
-
“如何设计主题和注释?” - 您粘贴的代码的方式将是一种方法。您可能会考虑查看具有提供程序的 AOSP 应用程序,以了解在这些应用程序中如何处理它们。在
android contentprovider "multiple tables"上进行 Google 搜索会发现更多想法,包括一些重复的 SO 问题,例如 stackoverflow.com/questions/13572352/… 和 stackoverflow.com/questions/3814005/…。 -
@pskink 看着
"vnd.android.cursor.dir/vnd.google.note"和"vnd.android.cursor.item/vnd.google.note"似乎光标是针对一张桌子的。
标签: android android-contentprovider