Android -- ContentProvider

之前写过一个sqlite的博客,传送门:《Android -- Android JUint 与 Sqlite》,这次写的Android四大组件之一的内容提供者,我拿ContentProvider从自己的程序里面读数据。所以需要用到以前的sqlite工程。

在sqlite工程中设置提供Content Provider                                    

新建一个类,继承于ContentProvider

在配置文件中设置:

<provider

            android:name="com.yuyidong.sqlite.PersonDBProvider"

            android:authorities="com.yuyidong.sqlite.personprovider" >

        </provider>
public class PersonDBProvider extends ContentProvider {

    

    private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int INSERT = 1;

    private static final int DELETE = 21;

    private static final int UPDATE = 3;

    private static final int QUERY = 4;

    private PersonSQLite sqlite;



    static {

        matcher.addURI("com.yuyidong.sqlite.personprovider", "insert", INSERT);

        matcher.addURI("com.yuyidong.sqlite.personprovider", "delete", DELETE);

        matcher.addURI("com.yuyidong.sqlite.personprovider", "update", UPDATE);

        matcher.addURI("com.yuyidong.sqlite.personprovider", "query", QUERY);

    }

    

    

    @Override

    public boolean onCreate() {

        sqlite = new PersonSQLite(getContext());

        return false;

    }



    @Override

    public Cursor query(Uri uri, String[] projection, String selection,

            String[] selectionArgs, String sortOrder) {

        if(matcher.match(uri) == QUERY)

        {

            SQLiteDatabase db = sqlite.getReadableDatabase();

            Cursor  cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);

            return cursor;

        }

        else

        {

            throw new IllegalArgumentException("路径不匹配,不能执行查询操作");

        }

        

    }



    @Override

    public String getType(Uri uri) {

        // TODO 自动生成的方法存根

        return null;

    }



    @Override

    public Uri insert(Uri uri, ContentValues values) {

        // TODO 自动生成的方法存根

        return null;

    }



    @Override

    public int delete(Uri uri, String selection, String[] selectionArgs) {

        // TODO 自动生成的方法存根

        return 0;

    }



    @Override

    public int update(Uri uri, ContentValues values, String selection,

            String[] selectionArgs) {

        // TODO 自动生成的方法存根

        return 0;

    }



}

这里static静态块里面的意思就是这个意思:content://com.yuyidong.sqlite.personprovider/insert

这个意思。  

新建工程,用来访问sqlite中的数据库                                             

private Button btn_query;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        btn_query = (Button) findViewById(R.id.btn_query);

        btn_query.setOnClickListener(new buttonLisnter());

    }



    class buttonLisnter implements OnClickListener {



        @Override

        public void onClick(View v) {

            ContentResolver resolver = getContentResolver();

            Uri uri = Uri

                    .parse("content://com.yuyidong.sqlite.personprovider/query");

            Cursor cursor = resolver.query(uri, null, null, null, null);

            while (cursor.moveToNext()) {

                String name = cursor.getString(cursor.getColumnIndex("name"));

                String id = cursor.getString(cursor.getColumnIndex("id"));

                System.out.println("name=" + name + "-----id=" + id);

            }



        }



    }

点击button之后获取数据。

image

我是天王盖地虎的分割线                                                                

源代码:http://pan.baidu.com/s/1dD1Qx01

ContentProvider4Sqlite1.zip

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut

你可能感兴趣的:(ContentProvider)