无法理解 UriMatcher 语法

Trouble understanding UriMatcher Syntax

我已经阅读了很多关于这个主题的资料,但我找不到任何关于这些符号 *, # 的意思的任何信息 be/represent。

如果它们被用作某些东西的占位符,例如 SqLite 的 ?,那么它们将替换什么?此外,如果您所做的只是匹配 Uri,那么使用这种神秘符号的目的是什么?

如果我想 return 一个列表、对象或其他任何东西,为什么我不直接用可读的东西命名常量并给它一个任意值,然后从那里继续?

例如

sURIMatcher.addURI("contacts", "people", PEOPLE);
sURIMatcher.addURI("contacts", "people/#", PEOPLE_ID);
sURIMatcher.addURI("contacts", "people/#/phones", PEOPLE_PHONES);

进入

sURIMatcher.addURI("contacts", "people", PEOPLE);
sURIMatcher.addURI("contacts", "people_id", PEOPLE_ID);
sURIMatcher.addURI("contacts", "people_phones", PEOPLE_PHONES);

我用它来删除我的数据库中具有特定 ID 的条目。

Uri deleteUri = Uri.parse(NoteProvider.CONTENT_URI + "/" + id);
context.getContentResolver().delete(deleteUri, null, null);

// URI Matcher
sURIMatcher.addURI(AUTHORITY, NOTE_BASE_PATH + "/#", NOTE_WITH_ID);

// delete
String id = uri.getLastPathSegment();
rowsDeleted = db.delete(NoteEntry.TABLE_NAME, NoteEntry._ID + "=" + id, null);
  • *:匹配任意长度任意有效字符的字符串。
  • #:匹配任意长度的数字字符串。

URI 表示资源的路径,这就是您使用 ContentProvider 访问的内容。所以我认为 URI 最适合这种情况。

A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table or file (a path). The optional id part points to an individual row in a table.