下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android——ContentProvider(一)創(chuàng)建contentProvider
一、在AndroidManifest.xml文件中添加
<provider
android:name=".StudentProvider"
android:authorities="com.example.android_contentprovider2.StudentProvider"
>
</provider>二、創(chuàng)建數(shù)據(jù)庫SqliteOpenHelperpublic
class
DbHelper
extends
SQLiteOpenHelper
{
private
static
String
name
=
"mydb.db";
private
static
int
version
=
1;//
初始的版本號(hào)是一
public
DbHelper(Context
context)
{
super(context,
name,
null,
version);
//
TODO
Auto-generated
constructor
stub
}
@Override
public
void
onCreate(SQLiteDatabase
database)
{
//
TODO
Auto-generated
method
stub
String
sql
=
"create
table
student
(id
integer
primary
key
autoincrement
,name
varchar(64)
,address
varchar(64))";
database.execSQL(sql);//
對(duì)數(shù)據(jù)庫的表的創(chuàng)建
}
@Override
public
void
onUpgrade(SQLiteDatabase
db,
int
oldVersion,
int
newVersion)
{
//
TODO
Auto-generated
method
stub
}
}三、創(chuàng)建ContentProviderpublic
class
StudentProvider
extends
ContentProvider
{
private
final
String
TAG
=
"StudentProvider";
private
DbHelper
helper
=
null;
private
static
final
UriMatcher
URI_MATCHER
=
new
UriMatcher(
UriMatcher.NO_MATCH);
private
static
final
int
STUDENT
=
1;//
操作單條記錄
private
static
final
int
STUDENTS
=
2;//
操作多條記錄
static
{
URI_MATCHER.addURI(
"com.example.android_contentprovider2.StudentProvider",
"student",
STUDENTS);
URI_MATCHER.addURI(
"com.example.android_contentprovider2.StudentProvider",
"student/#",
STUDENT);
}
public
StudentProvider()
{
//
TODO
Auto-generated
constructor
stub
}
@Override
public
int
delete(Uri
uri,
String
selection,
String[]
selectionArgs)
{
//
TODO
Auto-generated
method
stub
int
count
=
-1;//
影響數(shù)據(jù)庫的行數(shù)
try
{
int
flag
=
URI_MATCHER.match(uri);
SQLiteDatabase
database
=
helper.getWritableDatabase();
switch
(flag)
{
case
STUDENT:
//
content://com.example.android_contentprovider2.StudentProvider/student/1
//
delete
from
student
where
id
=
?
//id
通過客戶端傳遞過來的
long
id
=
ContentUris.parseId(uri);
String
where_value
=
"
id
=
"
+
id;
if
(selection
!=
null
&&
!selection.equals(""))
{
where_value
+=
"
and
"
+
selection;
}
count
=
database.delete("student",
where_value,
selectionArgs);
break;
case
STUDENTS:
count
=
database.delete("student",
selection,
selectionArgs);
break;
}
}
catch
(Exception
e)
{
//
TODO:
handle
exception
}
return
count;
}
@Override
public
String
getType(Uri
uri)
{
//
TODO
Auto-generated
method
stub
int
flag
=
URI_MATCHER.match(uri);
switch
(flag)
{
case
STUDENT:
return
"vnd.android.cursor.item/student";
case
STUDENTS:
return
"vnd.android.cursor.dir/students";
}
return
null;
}
@Override
public
Uri
insert(Uri
uri,
ContentValues
values)
{
//
TODO
Auto-generated
method
stub
//
insert
into
student
()
(?,?);
Uri
resultUri
=
null;
int
flag
=
URI_MATCHER.match(uri);
switch
(flag)
{
case
STUDENTS:
SQLiteDatabase
database
=
helper.getWritableDatabase();
long
id
=
database.insert("student",
null,
values);//
插入當(dāng)前行的行號(hào)
resultUri
=
ContentUris.withAppendedId(uri,
id);
break;
}
Log.i(TAG,
">>"
+
resultUri.toString());
return
resultUri;
}
@Override
public
boolean
onCreate()
{
//
TODO
Auto-generated
method
stub
helper
=
new
DbHelper(getContext());
return
true;
}
@Override
public
Cursor
query(Uri
uri,
String[]
projection,
String
selection,
String[]
selectionArgs,
String
sortOrder)
{
//
TODO
Auto-generated
method
stub
Cursor
cursor
=
null;
try
{
SQLiteDatabase
database
=
helper.getReadableDatabase();
int
flag
=
URI_MATCHER.match(uri);
switch
(flag)
{
case
STUDENT:
long
id
=
ContentUris.parseId(uri);
String
where_value
=
"
id
=
"
+
id;
if
(selection
!=
null
&&
!selection.equals(""))
{
where_value
+=
"
and
"
+
selection;
}
cursor
=
database.query("student",
null,
where_value,
selectionArgs,
null,
null,
null,
null);
break;
case
STUDENTS:
cursor
=
database.query("student",
null,
selection,
selectionArgs,
null,
null,
null);
break;
}
}
catch
(Exception
e)
{
//
TODO:
handle
exception
}
return
cursor;
}
@Override
public
int
update(Uri
uri,
ContentValues
values,
String
selection,
String[]
selectionArgs)
{
//
TODO
Auto-generated
method
stub
int
count
=
-1;
try
{
//
update
table
set
name
=
?
,address
=
?
where
id
=
?
SQLiteDatabase
database
=
helper.getWritableDatabase();
long
id
=
ContentUris.parseId(uri);
int
flag
=
URI_MATCHER.match(uri);
switch
(flag)
{
case
STUDENT:
String
where_value
=
"
id
=
"
+
id;
if
(selection
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 人教版初中語文七下《駱駝祥子》基礎(chǔ)復(fù)習(xí)必刷題(附答案)
- 2026年靈臺(tái)縣人民法院招聘?jìng)淇碱}庫有答案詳解
- 2026年西安長安大學(xué)工程設(shè)計(jì)研究院有限公司招聘?jìng)淇碱}庫含答案詳解
- 2026年欽州市交通運(yùn)輸局機(jī)關(guān)及局屬事業(yè)單位編外工作人員和“12328”熱線工作人員招聘8人備考題庫及參考答案詳解一套
- 2025年企業(yè)檔案管理與信息化手冊(cè)
- 2025年法律法規(guī)查詢與適用指南
- 2025年大學(xué)漢語言文學(xué)(現(xiàn)當(dāng)代文學(xué))試題及答案
- 2026年智慧物流倉儲(chǔ)機(jī)器人報(bào)告及未來五至十年供應(yīng)鏈優(yōu)化報(bào)告
- 2026年建筑行業(yè):3D打印結(jié)構(gòu)技術(shù)創(chuàng)新與綠色建筑行業(yè)創(chuàng)新報(bào)告
- 2025年鄉(xiāng)村文化節(jié)五年活動(dòng)風(fēng)險(xiǎn)管理報(bào)告
- 校外輔導(dǎo)員培訓(xùn)
- 2025年大學(xué)《應(yīng)急管理-應(yīng)急管理法律法規(guī)》考試參考題庫及答案解析
- 創(chuàng)意美術(shù)生蠔課件
- 2025年新版考監(jiān)控證的試題及答案
- 2025年上海市事業(yè)單位教師招聘體育學(xué)科專業(yè)知識(shí)考試
- 小學(xué)六年級(jí)英語重點(diǎn)語法全總結(jié)
- 基于低軌衛(wèi)星數(shù)據(jù)的熱層大氣密度反演:方法、挑戰(zhàn)與應(yīng)用
- 2025年國家開放大學(xué)《管理學(xué)基礎(chǔ)》期末考試備考試題及答案解析
- 黑龍江省安達(dá)市職業(yè)能力傾向測(cè)驗(yàn)事業(yè)單位考試綜合管理類A類試題帶答案
- (正式版)DB32∕T 5156-2025 《零碳園區(qū)建設(shè)指南》
- 2025年人教版八年級(jí)英語上冊(cè)各單元詞匯知識(shí)點(diǎn)和語法講解與練習(xí)(有答案詳解)
評(píng)論
0/150
提交評(píng)論