本文介绍了android.content.UriMatcher的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Uri Matcher in android.content.UriMatcher

What is Uri Matcher in android.content.UriMatcher

如何使用它?
有人可以解释以下三行代码的含义吗?

How to use it?Can someone please explain meaning of following three line of code?

  uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  uriMatcher.addURI(PROVIDER_NAME, "cte", uriCode);
  uriMatcher.addURI(PROVIDER_NAME, "cte/*", uriCode);
  int res = uriMatcher.match(uri);


推荐答案

当您编写ContentProvider时,UriMatcher是一个方便的类或者其他需要响应许多不同URI的类。在您的示例中,用户可以使用URI查询您的提供程序,例如:

UriMatcher is a handy class when you are writing a ContentProvider or some other class that needs to respond to a number of different URIs. In your example, a user could query your provider with URIs such as:

myprovider://cte

myprovider://cte/somestring

构建UriMatcher时,需要为每个URI分别设置代码(不只是像你的例子中的uriCode)。我通常将我的UriMatcher实例设置为静态,并在静态构造函数中添加URI:

When you construct a UriMatcher, you need to have separate codes for each URI (not just "uriCode" as in your example). I usually make my UriMatcher instance static, and add the URIs in a static constructor:

private static final int CTE_ALL = 1;
private static final int CTE_FIND = 2;
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
    uriMatcher.addURI(PROVIDER_NAME, "cte", CTE_ALL);
    uriMatcher.addURI(PROVIDER_NAME, "cte/*", CTE_FIND);
}

然后在您的ContentProvider中,您将在查询方法中执行以下操作:

Then in your ContentProvider you would do something like this in your query method:

Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    int res = uriMatcher.match(uri);
    switch (res) {
        case CTE_ALL:
            //TODO create a results Cursor with all the CTE results
            break;
        case CTE_FIND:
            //TODO create a results Cursor with the single CTE requested
            break;
    }
    return results;
}

这篇关于android.content.UriMatcher的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:24