ContentProvider
DataBaseOpenHelper
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36package org.wp.service; import org.wp.activity.ContentProviderMetaData; import org.wp.activity.ContentProviderMetaData.UserTableMetaData; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DataBaseOpenHelper extends SQLiteOpenHelper { private static final String TAG = "DataBaseOpenHelper"; private static final int VERSION = 1; public DataBaseOpenHelper(Context context, String name) { this(context, name, VERSION); } public DataBaseOpenHelper(Context context, String name, int version) { this(context, name, null, version); } public DataBaseOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { Log.i(TAG, "Create Database"); db.execSQL("create table " + ContentProviderMetaData.DATABASE_TABLE_NAME + "(" + UserTableMetaData._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + UserTableMetaData.USERS_NAME + " varchar(20)," + UserTableMetaData.USERS_AGE + " int)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i(TAG, "Update Database"); } }
ContentProviderMetaData
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35package org.wp.activity; import android.net.Uri; import android.provider.BaseColumns; /** * DATABASE_NAME:数据库名称 * DATABASE_VERSION:数据库版本 * DATABASE_TABLE_NAME:数据库表名 * * CONTENT_URI:访问该ContentProvider的URI * CONTENT_TYPE:该ContentProvider所返回的数据类型的定义 * CONTENT_TYPE_ITEM:该ContentProvider所返回的数据类型的定义 * TABLE_NAME:表名 * USERS_NAME:列名 * USERS_AGE:列名 * DEFAULT_SORT_ORDER:默认的排序方法 * * @author wp * */ public class ContentProviderMetaData { public static final String AUTHORITY = "org.wp.activity.ContentProviderCustom"; public static final String DATABASE_NAME = "contentProviderCustom_db"; public static final Integer DATABASE_VERSION = 1; public static final String DATABASE_TABLE_NAME = "users"; public static final class UserTableMetaData implements BaseColumns { public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/users"); public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.contentprovidercustom.user"; public static final String CONTENT_TYPE_ITEM = "vnd.android.cursor.item/vnd.contentprovidercustom.user"; public static final String TABLE_NAME = "users"; public static final String USERS_NAME = "name"; public static final String USERS_AGE = "age"; public static final String DEFAULT_SORT_ORDER = "_id desc"; } }
ContentProviderCustom
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119package org.wp.activity; import java.util.HashMap; import org.wp.activity.ContentProviderMetaData.UserTableMetaData; import org.wp.service.DataBaseOpenHelper; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.text.TextUtils; import android.util.Log; public class ContentProviderCustom extends ContentProvider { private static final String TAG = "ContentProviderCustom"; public static final UriMatcher uriMatcher; public static final int INCOMING_USER_COLLECTION = 1; public static final int INCOMINT_USER_SINGER = 2; public static HashMap<String, String> userProjectionMap; private DataBaseOpenHelper dboh; static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(ContentProviderMetaData.AUTHORITY, "users", INCOMING_USER_COLLECTION); uriMatcher.addURI(ContentProviderMetaData.AUTHORITY, "users/#", INCOMINT_USER_SINGER); userProjectionMap = new HashMap<String, String>(); userProjectionMap.put(UserTableMetaData._ID, UserTableMetaData._ID); userProjectionMap.put(UserTableMetaData.USERS_NAME, UserTableMetaData.USERS_NAME); userProjectionMap.put(UserTableMetaData.USERS_AGE, UserTableMetaData.USERS_AGE); } /* * 根据传入的URI,返回该URI所表示的数据类型 */ @Override public String getType(Uri uri) { Log.i(TAG, "getType()"); switch (uriMatcher.match(uri)) { case INCOMING_USER_COLLECTION: return UserTableMetaData.CONTENT_TYPE; case INCOMINT_USER_SINGER: return UserTableMetaData.CONTENT_TYPE_ITEM; default: throw new IllegalArgumentException("Unknown URI" + uri); } } /* * onCreate()是一个回调方法,在contentProvider创建的时候执行 */ @Override public boolean onCreate() { Log.i(TAG, "onCreate()"); dboh = new DataBaseOpenHelper(getContext(), ContentProviderMetaData.DATABASE_NAME); return true; } /* * 该函数的返回值是一个Uri,这个Uri表示刚刚使用这个函数所插入的数据 * content://org.wp.activity.ContentProviderCustom/users/1 */ @Override public Uri insert(Uri uri, ContentValues values) { Log.i(TAG, "insert()"); SQLiteDatabase sdb = dboh.getWritableDatabase(); Long rowId = sdb.insert(ContentProviderMetaData.DATABASE_TABLE_NAME, null, values); if (rowId > 0) { Uri insertUserUri = ContentUris.withAppendedId( UserTableMetaData.CONTENT_URI, rowId); getContext().getContentResolver().notifyChange(insertUserUri, null); return insertUserUri; } throw new SQLException("Failed to insert row into" + uri); } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.i(TAG, "query()"); SQLiteQueryBuilder sqb = new SQLiteQueryBuilder(); switch (uriMatcher.match(uri)) { case INCOMING_USER_COLLECTION: sqb.setTables(UserTableMetaData.TABLE_NAME); sqb.setProjectionMap(userProjectionMap); break; case INCOMINT_USER_SINGER: sqb.setTables(UserTableMetaData.TABLE_NAME); sqb.setProjectionMap(userProjectionMap); sqb.appendWhere(UserTableMetaData._ID + "=" + uri.getPathSegments().get(1)); break; } String orderBy; if (TextUtils.isEmpty(sortOrder)) { orderBy = UserTableMetaData.DEFAULT_SORT_ORDER; } else { orderBy = sortOrder; } SQLiteDatabase sdb = dboh.getWritableDatabase(); Cursor cur = sqb.query(sdb, projection, selection, selectionArgs, null, null, orderBy); cur.setNotificationUri(getContext().getContentResolver(), uri); return cur; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { Log.i(TAG, "delete()"); return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { Log.i(TAG, "update()"); return 0; } }
ContentProviderActivity
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56package org.wp.activity; import org.wp.activity.ContentProviderMetaData.UserTableMetaData; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; public class ContentProviderActivity extends Activity { private static final String TAG = "ContentProviderActivity"; private Button insertButton; private Button queryButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); insertButton = (Button) this.findViewById(R.id.insert); queryButton = (Button) this.findViewById(R.id.query); insertButton.setOnClickListener(listener); queryButton.setOnClickListener(listener); Log.i(TAG, getContentResolver().getType(UserTableMetaData.CONTENT_URI)); } private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { Button button = (Button) v; switch (button.getId()) { case R.id.insert: ContentValues values = new ContentValues(); values.put(UserTableMetaData.USERS_NAME, "张三"); values.put(UserTableMetaData.USERS_AGE, 10); Uri uri = getContentResolver().insert( UserTableMetaData.CONTENT_URI, values); Toast.makeText(ContentProviderActivity.this, uri.toString(), Toast.LENGTH_LONG).show(); Log.i(TAG, uri.toString()); break; case R.id.query: Cursor cur = getContentResolver().query( UserTableMetaData.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { Log.i(TAG, cur.getString(cur .getColumnIndex(UserTableMetaData.USERS_NAME))); Log.i(TAG, cur.getInt(cur .getColumnIndex(UserTableMetaData.USERS_AGE)) + ""); } break; } } }; }
最后
以上就是精明鲜花最近收集整理的关于Android学习笔记(十七)的全部内容,更多相关Android学习笔记(十七)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复