在库项目内容提供商

在库项目内容提供商

本文介绍了Android的,在库项目内容提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库项目为一些应用程序,所有需要相同的数据库结构和功能提供了基础code和我在寻找实现这一目标的最佳途径。
我最关心的是真正关心如何提供一个非静态的权威,所以我可以与使用该库的应用程序的包名覆盖它。

I have a library project providing the base code for a number of apps that all needs the same database structure and functionality and I'm looking for the best way to implement this.My main concern is really about how to provide a non static authority so I can override it with the package name of the app that uses the library.

因此​​,在库项目我想我可以改变以下常量静态方法将接受一个背景下,将让我从一个资源字符串或从context.getPackageName方法

So in the library project I was thinking I could change the following constants to static methods that will accept a context that will allow me to get the authority from a resource string or from the context.getPackageName method

库项目模型

    public static final class Questions implements BaseColumns {
//      public static final Uri CONTENT_URI =
//          Uri.parse("content://" + Config.AUTHORITY + "/questions");
//  use getAuthority instead which in turn will just get the package ae. Will this work?
        public static Uri getContentUri(Context c){
            return Uri.parse("content://" + getAuthority(c) + "/questions");
        }
    ...
    }

public static String getAuthority(Context c){
    return c.getPackageName + ".QuizProvider";
}

库项目内容提供商

library project content provider

public class QuizProvider extends ContentProvider {

//    private SQLiteDatabase mDB;

    private static final String TAG = "MyLog";

    private static final String DATABASE_NAME = "quiz.db";

    private static final int DATABASE_VERSION = 1;

    private static final int QUESTIONS = 1;
    private static final int QUESTION_ID = 2;

    private static final UriMatcher URI_MATCHER = new UriMatcher(
        UriMatcher.NO_MATCH);

    private static Map<String, String> QUESTION_LIST_PROJECTION_MAP = null;

    private DatabaseHelper mOpenHelper;

    /**
     * This class helps open, create, and upgrade the database file.
     */

    private static class DatabaseHelper extends SQLiteOpenHelper {

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int match = URI_MATCHER.match(uri);
        switch (match) {
        case QUESTIONS:
            return insertQuestion(values);
        ...
        }

...
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        switch (URI_MATCHER.match(uri)) {
        case QUESTIONS:
            qb.setTables(QuizModel.Questions.TABLE_NAME);
            qb.setProjectionMap(QUESTION_LIST_PROJECTION_MAP);
            break;
...
        }

//How do I change this?
        static {
            URI_MATCHER.addURI(Config.AUTHORITY, QuizModel.Questions.TABLE_NAME, QUESTIONS);
    ...
            QUESTION_LIST_PROJECTION_MAP = new HashMap<String, String>();
    ...
        }

1)我真的不明白静态{} URI_MATCHER.addUR申报和我在努力理解如何我可以转换呢?

1) I don't really understand the static { URI_MATCHER.addUR } declaration and am struggling to understand how I can convert this?

2),这是最好的办法是能够重复使用任何应用程序的整个功能?如果不是会更好?

2) Is this the best approach to be able to reuse the whole functionality in whatever app? If not what would be better?

推荐答案

请参阅此 ......我相信要回答你所有的问题(或者至少给你一个更好地了解如何处理这个)。

See this answer... I believe it should answer all of your questions (or at least give you a better understanding of how to approach this).

这篇关于Android的,在库项目内容提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 07:39