ContentProvider 开发之简单学习

package com.test.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * MyContentProvider
 */
public class MyContentProvider extends ContentProvider {
    public MyContentProvider() {
    }

    //第一次创建该 ContentProvider 时调用该方法
    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.


        Log.d("MyContentProvider", "onCreate方法被调用");

        return true;
    }

    //该方法的返回值代表了该 ContentProvider所提供数据的 MIME类型
    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        //   throw new UnsupportedOperationException("Not yet implemented");
        return null;
    }

    //实现查询方法该方法返回查询得到的cursor

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        Log.d("MyContentProvider", uri + " query方法被调用");
        Log.d("MyContentProvider", "selection: " + selection);
        return null;
    }

    //实现插入法 该方法返回新插入记录的 uri
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.d("MyContentProvider", uri + " insert方法被调用");
        Log.d("MyContentProvider", "values: " + values);

        return null;
    }

    //实现删除方法 该方法返回被删除的记录条数
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        Log.d("MyContentProvider", uri + " delete 方法被调用");
        Log.d("MyContentProvider", "selection: " + selection);

        return 0;

    }


    //实现更新方法 该方法返回被更新的记录条数
    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        Log.d("MyContentProvider", uri + " update 方法被调用");
        Log.d("MyContentProvider", "values: " + values);
        Log.d("MyContentProvider", "selection: " + selection);

        return 0;
    }
}

ContentResolver


package com.test.contentprovider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/**
 * ContentResolver
 */
public class MyResolverActivity extends AppCompatActivity {
    ContentResolver mContentResolver;
    Uri uri = Uri.parse("content://cn.itcast.provider/");

    Button insert, update, query, delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_resolver);

        mContentResolver = getContentResolver();

        initView();

        initListener();
    }

    private void initView() {
        insert = (Button) findViewById(R.id.insert);
        delete = (Button) findViewById(R.id.delete);
        update = (Button) findViewById(R.id.update);
        query = (Button) findViewById(R.id.query);
    }

    private void initListener() {
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用 ContentResolver的 query()方法
                //实际是返回的是该 uri 对应的 ContentProvider的 query()方法
                Cursor cursor = mContentResolver.query(uri, null, "query_where", null, null);
                Toast.makeText(MyResolverActivity.this, "远程ContentResolver的返回的cursor为 : " + cursor, Toast.LENGTH_SHORT).show();
            }
        });

        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values = new ContentValues();
                values.put("name", "android");
                // 调用ContentResolver的insert()方法。
                // 实际返回的是该Uri对应的ContentProvider的insert()的返回值
                Uri newUri = mContentResolver.insert(uri, values);
                Toast.makeText(MyResolverActivity.this, "远程ContentProvide新插入记录的Uri为:" + newUri, Toast.LENGTH_LONG).show();
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values = new ContentValues();
                values.put("name", "android");
                // 调用ContentResolver的update()方法。
                // 实际返回的是该Uri对应的ContentProvider的update()的返回值
                int count = mContentResolver.update(uri, values
                        , "update_where", null);
                Toast.makeText(MyResolverActivity.this, "远程ContentProvide更新记录数为:"
                        + count, Toast.LENGTH_LONG).show();
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 调用ContentResolver的delete()方法。
                // 实际返回的是该Uri对应的ContentProvider的delete()的返回值
                int count = mContentResolver.delete(uri
                        , "delete_where", null);
                Toast.makeText(MyResolverActivity.this, "远程ContentProvide删除记录数为:"
                        + count, Toast.LENGTH_LONG).show();
            }
        });
    }
}

布局界面

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.test.contentprovider.MyResolverActivity">

    <Button
        android:id="@+id/insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/insert"
        />
    <Button
        android:id="@+id/query"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/query"
        />
    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/delete"
        />
    <Button
        android:id="@+id/update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/update"
        />

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值