码迷,mamicode.com
首页 > 其他好文 > 详细

ContentProvider中的数据生成时机

时间:2015-09-08 23:34:26      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

 

MainActivity.java

 1 package com.wyl.contentprovidermine;
 2 
 3 
 4 import android.app.Activity;
 5 import android.content.ContentResolver;
 6 import android.content.ContentValues;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 public class MainActivity extends Activity implements OnClickListener{
12     Button btn_insert;
13     Button btn_select;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         btn_insert = (Button) findViewById(R.id.btn_insert);
19         btn_select = (Button) findViewById(R.id.btn_select);
20         btn_insert.setOnClickListener(this);
21     }
22     @Override
23     public void onClick(View v) {
24         switch (v.getId()) {
25         case R.id.btn_insert:
26             ContentResolver cr = getContentResolver();
27             ContentValues values = new ContentValues();
28             values.put(myMetaData.UserTableMetaData.NAME, "zyl");
29             values.put(myMetaData.UserTableMetaData.AGE, 21);
30             values.put(myMetaData.UserTableMetaData.SEX, "女");
31 //            Uri uri = new URI()  
32             System.out.println("点了insert按钮......");
33             cr.insert(myMetaData.UserTableMetaData.CONTENT_URI, values);
34             System.out.println("点了insert按钮------------------");
35             break;
36 
37         case R.id.btn_select:
38             System.out.println("查询数据......");
39             break;
40         }
41     }
42 }

 

myContentProvider.java

  1 package com.wyl.contentprovidermine;
  2 
  3 import android.content.ContentProvider;
  4 import android.content.ContentUris;
  5 import android.content.ContentValues;
  6 import android.content.UriMatcher;
  7 import android.database.Cursor;
  8 import android.database.sqlite.SQLiteDatabase;
  9 import android.net.Uri;
 10 
 11 public class myContentProvider extends ContentProvider{
 12     mySqliteHelper helper;
 13     SQLiteDatabase db;
 14     private static final UriMatcher myUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
 15     public static final int USERS = 1;//代表表名
 16     public static final int USERS_NAME = 2; //字段名
 17     public static final int USERS_SEX = 3;  //字段名 ,性别
 18     public static final int USERS_AGE = 4;  //字段名,年龄
 19     public static final int USERS_SINGLE = 5;
 20     static{
 21         System.out.println("myContentProvider.static{} 静态代码块。。。。");
 22         myUriMatcher.addURI(myMetaData.AUTHORITY, "/users", USERS);//匹配表名
 23         myUriMatcher.addURI(myMetaData.AUTHORITY, "/users/name", USERS_NAME);
 24         myUriMatcher.addURI(myMetaData.AUTHORITY, "/users/sex", USERS_SEX);
 25         myUriMatcher.addURI(myMetaData.AUTHORITY, "/users/age", USERS_AGE);//只能匹配age这个字段
 26         myUriMatcher.addURI(myMetaData.AUTHORITY, "/users/#", USERS_SINGLE);//这个可以匹配任何字段名
 27     }
 28     
 29     
 30     @Override
 31     public boolean onCreate() {
 32         // TODO Auto-generated method stub
 33         /*
 34          * 创建数据库,同时也会生成表,见mySqliteHelper的onCreate()方法,
 35          * 这个方法里有创建表的代码
 36          */
 37         /*
 38          * 如果这行代码(helper = new mySqliteHelper(getContext(), "zhangyalan.db");)注释掉了,那么当
 39          * 插入数据的时候执行下面的insert方法的时候就会空指针异常,因为在清单文件中配置了,所以这个android app安装的时候,就
 40          * 会执行这个类,即 myContentProvider.java ,而且 在这个类里,首先执行静态代码块里的代码,然后执
 41          * 行本方法myContentProvider.onCreate(),本类的其他方法,如insert(),query()等方法,只有用
 42          * 户在手机界面进行操作的时候才会执行到。知道了这里的执行顺序那么就很容易理解为什么下面的这行代码注释掉了就会导
 43          * 致用户插入数据的时候会导致空指针异常了(因为 helper没有实例化),自己写一个ContentProvider的时候要首先实例化
 44          * 这里就提示了我们很重要的一点,即实例化SQLiteOpenHelper的时机一定要早,比如放到静态代码块或者onCreate()方法里
 45          */
 46         helper = new mySqliteHelper(getContext(), "zhangyalan.db");//这行代码千万要实例化
 47         if(helper!=null){
 48             System.out.println("helper实际上还没实例化");
 49         }else{
 50             System.out.println("helper实际上已经实例化了。。。");
 51         }
 52         System.out.println("myContentProvider.onCreate()方法,  jianli le zhangyalan.db");
 53         return true;
 54     }
 55 
 56     @Override
 57     public Cursor query(Uri uri, String[] projection, String selection,
 58             String[] selectionArgs, String sortOrder) {
 59         // TODO Auto-generated method stub
 60         return null;
 61     }
 62     //作用:根据传入的URI,返回该URI所表示的数据类型
 63     @Override
 64     public String getType(Uri uri) {
 65         // TODO Auto-generated method stub
 66         System.out.println("public String getType(Uri uri) 开始了。。。。。。");
 67         switch (myUriMatcher.match(uri)) {
 68         case USERS:
 69             return myMetaData.UserTableMetaData.CONTENT_TYPE;
 70             
 71         case USERS_SINGLE:
 72             return myMetaData.UserTableMetaData.CONTENT_TYPE_ITEM;
 73         default:
 74             throw new IllegalArgumentException("未知的uri,unknow URI..."+uri);
 75         }
 76     }
 77 
 78     @Override
 79     public Uri insert(Uri uri, ContentValues values) {
 80         System.out.println("myContentProvider.insert()......1 ");
 81         db = helper.getWritableDatabase();
 82         System.out.println("myContentProvider.insert()......2 ");
 83         long rowId = db.insert(myMetaData.UserTableMetaData.TABLE_NAME, null, values);
 84         if(rowId>0){
 85             Uri rtnUri = ContentUris.withAppendedId(uri, rowId);
 86             System.out.println("myContentProvider.insert()......3 ");
 87             return rtnUri;
 88         }
 89         System.out.println("myContentProvider.insert()......4 ");
 90         return null;
 91     }
 92 
 93     @Override
 94     public int delete(Uri uri, String selection, String[] selectionArgs) {
 95         // TODO Auto-generated method stub
 96         return 0;
 97     }
 98 
 99     @Override
100     public int update(Uri uri, ContentValues values, String selection,
101             String[] selectionArgs) {
102         // TODO Auto-generated method stub
103         return 0;
104     }
105 
106 }

 

mySqliteHelper.java

 1 package com.wyl.contentprovidermine;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 6 import android.database.sqlite.SQLiteOpenHelper;
 7 
 8 public class mySqliteHelper extends SQLiteOpenHelper{
 9     
10     static{
11         
12         System.out.println("==看看实例化本类的时候,是否会执行到我.....==");
13     }
14     public mySqliteHelper(Context context, String name, CursorFactory factory,
15             int version) {
16         super(context, name, factory, version);
17         // TODO Auto-generated constructor stub
18     }
19     public mySqliteHelper(Context context, String name, 
20             int version) {
21         super(context, name, null, version);
22         // TODO Auto-generated constructor stub
23     }
24     
25     /**
26      * 两个参数的构造器,用来创建数据库
27      * @param context  activity
28      * @param name          数据库名
29      */
30     public mySqliteHelper(Context context, String name) {
31         this(context, name, 1);
32         // TODO Auto-generated constructor stub
33     }
34     
35     /**
36      *  这个方法主要是用来创建 表的,
37      *  现在的疑问是 数据库是是什么时候创建的:实际上数据库是实例化该MySqliteHelper的时候
38      *  创建,
39      */
40     @Override
41     public void onCreate(SQLiteDatabase db) {
42         // TODO Auto-generated method stub
43         System.out.println("mySqliteHelper onCreate(SQLiteDatabase db)方法......");
44         db.execSQL(myMetaData.UserTableMetaData.CREATE_TABLE_SQL);//创建表
45     }
46 
47     @Override
48     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
49         // TODO Auto-generated method stub
50         
51     }
52 
53 }

 

myMetaData.java

 1 package com.wyl.contentprovidermine;
 2 
 3 import android.net.Uri;
 4 import android.provider.BaseColumns;
 5 
 6 public class myMetaData {
 7     //AUTHORITY 是一个类名,即contentprovider的类名
 8     public static final String AUTHORITY = "com.wyl.contentprovidermine";
 9     //数据库名称
10     public static final String DATABASE_NAME = "wyl.db";
11     //表名
12     public static final String USER_TABLE_NAME = "users";
13     
14     public static class UserTableMetaData implements BaseColumns{
15         //字表名称
16         public static final String TABLE_NAME = "users";
17         
18         public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/users");
19                                                         //1 content 2 AUTHORITY 3.字标的名字
20         public static final String NAME = "name";
21         public static final String SEX = "sex";
22         public static final String AGE = "age";
23         
24         public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.myprovider.users";
25         //上面的 CONTENT_TYPE: 其中的 vnd.android.cursor.dir/vnd 是固定的, 后面的 myprovider.users 自己随便定义
26         public static final String CONTENT_TYPE_ITEM = "vnd.android.cursor.item/vnd.myprovider.users";
27     
28         public static final String DEFAULT_SORT_ORDER = "_id desc";
29         
30         public static final String CREATE_TABLE_SQL = "create table if not exists "+TABLE_NAME + " (_id integer primary key autoincrement,name text not null,sex text,age integer not null) ";
31     }
32     
33     
34 }

 

ContentProvider中的数据生成时机

标签:

原文地址:http://www.cnblogs.com/Sunnor/p/4793261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!