2016-11-04 3 views
0

複雑なクエリを使用してコンテンツプロバイダを実装するには、複雑なURIパターンが必要でしたが、これをどのように処理できるのか分かりません。Androidで高度なUriパターンを処理する方法

  1. 私のパターンに正規表現を使用できますか?例:/user/:[a-zA-Z]/timeline/ユーザーが文字のみを含む必要がある場合。
  2. UriMatherにどのようなパラメータの例を示すシンボル:/user/:userId/timeline/year/:yearNumber、私はパラメータとしてuserIdとyearNumberを取得するので、どのように値を取得する必要がありますか? getPathSegments()を使用して手動でパラメータを取得する必要がありますか?
  3. または私は/user/#/timeline/year/#を使用した場合、私はフォーマット/user/#/timeline/year/#を使用することにより#値

答えて

0

を抽出んどのように呼び出すことができます。

uri.getPathSegments().get(1); // To get the first # 
uri.getPathSegments().get(4); // to get the second # 
// 0 -> user, 1 -> first #, 2 -> timeline, 3 -> year, 4 -> second # 

をだからあなたのコンテンツプロバイダにあなたはこのようなものだろう:

をチェック https://developer.android.com/guide/topics/providers/content-provider-creating.html
private static final int USER_TIMELINE_YEAR = 1; 
// ... 

private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
static 
{ 
    uriMatcher.addURI(PROVIDER_NAME, "user/#/timeline/year/#", USER_TIMELINE_YEAR); 
} 

// Usually a ContentProvider method like query, insert, delete and update 
public void someMethod(Uri uri) { 
    if(uriMatcher.match(uri) == USER_TIMELINE_YEAR) { 
     String userId = uri.getPathSegments().get(1); 
     String timelineYear = uri.getPathSegments().get(4); 
    } 
} 

https://developer.android.com/reference/android/content/UriMatcher.html

+0

okありがとうございます。正規表現に関する最初の質問はどうですか? – Lior

+0

いいえ、正規表現は使用できません。任意の数字には#、任意の文字列には*を使用できます。 –

関連する問題