1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<application android:icon=
"@drawable/icon"
android:label=
"@string/app_name"
>
<activity android:name=
".TestWebviewDemo"
android:label=
"@string/app_name"
>
<intent-filter>
<action android:name=
"android.intent.action.MAIN"
/>
<category android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
<intent-filter>
<data android:mimeType=
"vnd.android.cursor.dir/vnd.ruixin.login"
/>
</intent-filter>
<intent-filter>
<data android:mimeType=
"vnd.android.cursor.item/vnd.ruixin.login"
/>
</intent-filter>
</activity>
<provider android:name=
"MyProvider"
android:authorities=
"com.ruixin.login"
/>
</application>
|
須要在<application></application>中爲provider進行註冊!!!!
首先定義一個數據庫的工具類:html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public
class
RuiXin {
public
static
final
String DBNAME =
"ruixinonlinedb"
;
public
static
final
String TNAME =
"ruixinonline"
;
public
static
final
int
VERSION =
3
;
public
static
String TID =
"tid"
;
public
static
final
String EMAIL =
"email"
;
public
static
final
String USERNAME =
"username"
;
public
static
final
String DATE =
"date"
;
public
static
final
String SEX =
"sex"
;
public
static
final
String AUTOHORITY =
"com.ruixin.login"
;
public
static
final
int
ITEM =
1
;
public
static
final
int
ITEM_ID =
2
;
public
static
final
String CONTENT_TYPE =
"vnd.android.cursor.dir/vnd.ruixin.login"
;
public
static
final
String CONTENT_ITEM_TYPE =
"vnd.android.cursor.item/vnd.ruixin.login"
;
}
|
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
|
public
class
DBlite
extends
SQLiteOpenHelper {
public
DBlite(Context context) {
super
(context, RuiXin.DBNAME,
null
, RuiXin.VERSION);
// TODO Auto-generated constructor stub
}
@Override
public
void
onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table "
+RuiXin.TNAME+
"("
+
RuiXin.TID+
" integer primary key autoincrement not null,"
+
RuiXin.EMAIL+
" text not null,"
+
RuiXin.USERNAME+
" text not null,"
+
RuiXin.DATE+
" interger not null,"
+
RuiXin.SEX+
" text not null);"
);
}
@Override
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion) {
// TODO Auto-generated method stub
}
public
void
add(String email,String username,String date,String sex){
SQLiteDatabase db = getWritableDatabase();
ContentValues values =
new
ContentValues();
values.put(RuiXin.EMAIL, email);
values.put(RuiXin.USERNAME, username);
values.put(RuiXin.DATE, date);
values.put(RuiXin.SEX, sex);
db.insert(RuiXin.TNAME,
""
,values);
}
}
|
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
|
public
class
MyProvider
extends
ContentProvider{
DBlite dBlite;
SQLiteDatabase db;
private
static
final
UriMatcher sMatcher;
static
{
sMatcher =
new
UriMatcher(UriMatcher.NO_MATCH);
sMatcher.addURI(RuiXin.AUTOHORITY,RuiXin.TNAME, RuiXin.ITEM);
sMatcher.addURI(RuiXin.AUTOHORITY, RuiXin.TNAME+
"/#"
, RuiXin.ITEM_ID);
}
@Override
public
int
delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
db = dBlite.getWritableDatabase();
int
count =
0
;
switch
(sMatcher.match(uri)) {
case
RuiXin.ITEM:
count = db.delete(RuiXin.TNAME,selection, selectionArgs);
break
;
case
RuiXin.ITEM_ID:
String id = uri.getPathSegments().get(
1
);
count = db.delete(RuiXin.TID, RuiXin.TID+
"="
+id+(!TextUtils.isEmpty(RuiXin.TID=
"?"
)?
"AND("
+selection+
')'
:
""
), selectionArgs);
break
;
default
:
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
getContext().getContentResolver().notifyChange(uri,
null
);
return
count;
}
@Override
public
String getType(Uri uri) {
// TODO Auto-generated method stub
switch
(sMatcher.match(uri)) {
case
RuiXin.ITEM:
return
RuiXin.CONTENT_TYPE;
case
RuiXin.ITEM_ID:
return
RuiXin.CONTENT_ITEM_TYPE;
default
:
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
}
@Override
public
Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
db = dBlite.getWritableDatabase();
long
rowId;
if
(sMatcher.match(uri)!=RuiXin.ITEM){
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
rowId = db.insert(RuiXin.TNAME,RuiXin.TID,values);
if
(rowId>
0
){
Uri noteUri=ContentUris.withAppendedId(RuiXin.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(noteUri,
null
);
return
noteUri;
}
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
@Override
public
boolean
onCreate() {
// TODO Auto-generated method stub
this
.dBlite =
new
DBlite(
this
.getContext());
// db = dBlite.getWritableDatabase();
// return (db == null)?false:true;
return
true
;
}
@Override
public
Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
db = dBlite.getWritableDatabase();
Cursor c;
Log.d(
"-------"
, String.valueOf(sMatcher.match(uri)));
switch
(sMatcher.match(uri)) {
case
RuiXin.ITEM:
c = db.query(RuiXin.TNAME, projection, selection, selectionArgs,
null
,
null
,
null
);
break
;
case
RuiXin.ITEM_ID:
String id = uri.getPathSegments().get(
1
);
c = db.query(RuiXin.TNAME, projection, RuiXin.TID+
"="
+id+(!TextUtils.isEmpty(selection)?
"AND("
+selection+
')'
:
""
),selectionArgs,
null
,
null
, sortOrder);
break
;
default
:
Log.d(
"!!!!!!"
,
"Unknown URI"
+uri);
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return
c;
}
@Override
public
int
update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return
0
;
}
}
|
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
|
public
class
Test
extends
Activity {
/** Called when the activity is first created. */
private
DBlite dBlite1 =
new
DBlite(
this
);;
private
ContentResolver contentResolver;
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
//先對數據庫進行添加數據
dBlite1.add(email,username,date,sex);
//經過contentResolver進行查找
contentResolver = TestWebviewDemo.
this
.getContentResolver();
Cursor cursor = contentResolver.query(
RuiXin.CONTENT_URI,
new
String[] {
RuiXin.EMAIL, RuiXin.USERNAME,
RuiXin.DATE,RuiXin.SEX },
null
,
null
,
null
);
while
(cursor.moveToNext()) {
Toast.makeText(
TestWebviewDemo.
this
,
cursor.getString(cursor.getColumnIndex(RuiXin.EMAIL))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.USERNAME))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.DATE))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.SEX)),
Toast.LENGTH_SHORT).show();
}
startManagingCursor(cursor);
//查找後關閉遊標
}
}
|
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
|
public
class
Test
extends
Activity {
/** Called when the activity is first created. */
private
ContentResolver contentResolver;
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
//經過contentResolver進行查找
contentResolver = TestWebviewDemo.
this
.getContentResolver();
Cursor cursor = contentResolver.query(
RuiXin.CONTENT_URI,
new
String[] {
RuiXin.EMAIL, RuiXin.USERNAME,
RuiXin.DATE,RuiXin.SEX },
null
,
null
,
null
);
while
(cursor.moveToNext()) {
Toast.makeText(TestWebviewDemo.
this
,
cursor.getString(cursor.getColumnIndex(RuiXin.EMAIL))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.USERNAME))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.DATE))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.SEX)),
Toast.LENGTH_SHORT).show();
}
startManagingCursor(cursor);
//查找後關閉遊標
}
}
|