Android 内容提供者 一个查询内容小demo

Android 内容提供者


以下是demo的代码:
首先 先将下面的项目放到Android Studio里:
http://pan.baidu.com/s/1kUMYMGn

是个压缩包 (其实压缩包里已经创建好了)
在项目的java路径下面新建一个新得文件夹 com.dzz.provider
在里面在新建一个MyProvider 文件

MyProvider 文件代码:

package com.dzz.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;

import com.dzz.db.DbHelper;

/**
 * Created by 朝花偏不夕拾 on 2017/2/14.
 */

public class MyProvider extends ContentProvider {

    private SQLiteDatabase sqLiteDatabase;
    private UriMatcher uriMatcher;

    private static final int PERSONS=1;
    private static final int PERSON=2;

    @Override
    public boolean onCreate() {
        Log.i("test","onCreate");

        DbHelper dbHelper=new DbHelper(getContext(),"question.db",null,2);
        sqLiteDatabase = dbHelper.getReadableDatabase();

        //实例化URL匹配器
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //添加匹配规则
        //http://localhost:8080/xxx/000.action
    //规则 :
//        content://com.dzz.android24_sqlite.person/persons   //查询单个
//        content://com.dzz.android24_sqlite.person/persons/#   //条件查询
        uriMatcher.addURI("com.dzz.android24_sqlite.person","persons",MyProvider.PERSONS);
        uriMatcher.addURI("com.dzz.android24_sqlite.person","persons/#",MyProvider.PERSON);

        return false;
    }

    @Nullable
    @Override   //Cursor  是个结果
    public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
        Log.i("test","query");

        int code= uriMatcher.match(uri);
        switch (code) {
            case MyProvider.PERSONS:
                Log.i("test","query所有");
                //查询所有的数据
                return sqLiteDatabase.query(true,"person",strings,s,strings1,null,null,s1,null);
            case MyProvider.PERSON:
                Log.i("test","query单个");
                //获取#的值
                long cc= ContentUris.parseId(uri);
                return sqLiteDatabase.rawQuery("select * from person where _id=?",new String[]{cc+""});
        }
        return null;

    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        Log.i("test","getType");
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        Log.i("test","insert");
        return null;
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {
        Log.i("test","delete");
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
        Log.i("test","update");
        return 0;
    }
}

还要在这个项目的清单文件配置 提供者

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dzz.android24_sqlite">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--配置提供者内容-->
        <provider
            android:authorities="com.dzz.android24_sqlite.person"
            android:name="com.dzz.provider.MyProvider"
            android:exported="true"
        ></provider>

    </application>

</manifest>

然后在新建一个项目 因为我们要取到上一个项目里的值

先写下XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.dzz.android24_contentresolver.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_main"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取数据"
        android:onClick="getData"
        />
</LinearLayout>

最后给获取数据加上点击 可以查看你取到值了没有

package com.dzz.android24_contentresolver;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ContentResolver cr;
    private EditText et_main;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_main = (EditText) findViewById(R.id.et_main);

        //获取内容访问者
        cr =getContentResolver();
    }

    public void getData(View view){
        if(TextUtils.isEmpty(et_main.getText())){
            //查询所有
            //              //前面必须要加content://
            uri = Uri.parse("content://com.dzz.android24_sqlite.person/persons");
        }else{
            //查询单个
            String ss=et_main.getText().toString();
            uri = Uri.parse("content://com.dzz.android24_sqlite.person/persons/"+ss);

        }


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

        //查询单个
//        1.   条件  id=1;
//        2.   网页:
//        3.   URL 匹配器

        while(cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name=cursor.getString(cursor.getColumnIndex("name"));
            int age=cursor.getInt(cursor.getColumnIndex("age"));
            Log.i("test",id+"___"+name+"+++"+age);
            Toast.makeText(this, id+"___"+name+"+++"+age, Toast.LENGTH_SHORT).show();
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值