高级程序员修炼之道-Android培训系列课程之ContentProvider5

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

-AndroidContentProvider://(client)AndroidContentProviderContentProviderSQLiteContentProviderContentProvidersDecideifyouneedacontentprovider.Youneedtobuildacontentproviderifyouwanttoprovideoneormoreofthefollowingfeatures:Youwanttooffercomplexdataorfilestootherapplications.Youwanttoallowuserstocopycomplexdatafromyourappintootherapps.Youwanttoprovidecustomsearchsuggestionsusingthesearchframework.ContentProviders1.2.-AndroidContentProvider://authority/path/idpathididContentProviderURI[java]viewplaincopyprint?1.publicclassExampleProviderextendsContentProvider{...2.//CreatesaUriMatcherobject.3.privatestaticfinalUriMatchersUriMatcher;...4./*5.*ThecallstoaddURI()gohere,forallofthecontentURIpatternsthattheprovider6.*shouldrecognize.Forthissnippet,onlythecallsfortable3areshown.7.*/...8./*9.*Setstheintegervalueformultiplerowsintable3to1.Noticethatnowildcardisused10.*inthepath11.*/12.sUriMatcher.addURI(com.example.app.provider,table3,1);13.14./*-AndroidContentProvider*Setsthecodeforasinglerowto2.Inthiscase,the#wildcardis16.*used.content://com.example.app.provider/table3/3matches,but17.*content://com.example.app.provider/table3doesn't.18.*19.**:Matchesastringofanyvalidcharactersofanylength.20.*#:Matchesastringofnumericcharactersofanylength.21.22.*/23.sUriMatcher.addURI(com.example.app.provider,table3/#,2);...24.//ImplementsContentProvider.query()25.publicCursorquery(26.Uriuri,27.String[]projection,28.Stringselection,29.String[]selectionArgs,30.StringsortOrder){...31./*32.*Choosethetabletoqueryandasortorderbasedonthecodereturnedfortheincoming33.*URI.Here,too,onlythestatementsfortable3areshown.34.*/35.switch(sUriMatcher.match(uri)){36.37.38.//IftheincomingURIwasforalloftable339.case1:40.41.if(TextUtils.isEmpty(sortOrder))sortOrder=_IDASC;42.break;43.44.//IftheincomingURIwasforasinglerow45.case2:46.47./*48.*BecausethisURIwasforasinglerow,the_IDvaluepartis49.*present.GetthelastpathsegmentfromtheURI;thisisthe_IDvalue.-AndroidContentProvider*Then,appendthevaluetotheWHEREclauseforthequery51.*/52.selection=selection+_ID=uri.getLastPathSegment();53.break;54.55.default:56....57.//IftheURIisnotrecognized,youshoulddosomeerrorhandlinghere.58.}59.//callthecodetoactuallydothequery}ContentProviderquery()Retrievedatafromyourprovider.Usetheargumentstoselectthetabletoquery,therowsandcolumnstoreturn,andthesortorderoftheresult.ReturnthedataasaCursorobject.insert()Insertanewrowintoyourprovider.Usetheargumentstoselectthedestinationtableandtogetthecolumnvaluestouse.ReturnacontentURIforthenewly-insertedrow.update()Updateexistingrowsinyourprovider.Usetheargumentstoselectthetableandrowstoupdateandtogettheupdatedcolumnvalues.Returnthenumberofrowsupdated.delete()Deleterowsfromyourprovider.Usetheargumentstoselectthetableandtherowstodelete.Returnthenumberofrowsdeleted.getType()ReturntheMIMEtypecorrespondingtoacontentURI.ThismethodisdescribedinmoredetailinthesectionImplementingContentProviderMIMETypes.onCreate()Initializeyourprovider.TheAndroidsystemcallsthismethodimmediatelyafteritcreatesyourprovider.NoticethatyourproviderisnotcreateduntilaContentResolverobjecttriestoaccessit.SQLiteContentProviderSQLiteOpenHelperMainDatabaseHelpermain[java]viewplaincopyprint?-AndroidContentProvider=CREATETABLE+2.main+//Table'sname3.(+//Thecolumnsinthetable4._IDINTEGERPRIMARYKEY,+5.WORDTEXT6.FREQUENCYINTEGER+7.LOCALETEXT);.../**8.*Helperclassthatactuallycreatesandmanagestheprovider'sunderlyingdatarepository.9.*/protectedstaticfinalclassMainDatabaseHelperextendsSQLiteOpenHelper{10.11./*12.*Instantiatesanopenhelperfortheprovider'sSQLitedatarepository13.*Donotdodatabasecreationandupgradehere.14.*/15.MainDatabaseHelper(Contextcontext){16.super(context,DBNAME,null,1);17.}18.19./*20.*Createsthedatarepository.Thisiscalledwhentheproviderattemptstoopenthe21

1 / 21
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功