Learn from Android under ContentProvider

Source: Internet
Author: User
<span id="Label3"></p><p><p>Using ContentProvider</p></p><p><p>1, the first to inherit ContentProvider</p></p><p><p>2. To declare contentprovider in the configuration file</p></p><p><p><permission android:name= "aa.bb.cc.read" ></permission></p></p><p><p><permission android:name= "aa.bb.cc.write" ></permission></p></p><p><p><provider</p></p><p><p>Android:name= ". Providers. Personcontentprovider "</p></p><p><p>android:authorities= "com.itheima28.sqlitedemo.providers.PersonContentProvider"</p></p><p><p>android:readpermission= "aa.bb.cc.read"</p></p><p><p>android:writepermission= "aa.bb.cc.write" ></p></p><p><p></provider></p></p><p><p><br></p></p><p><p><br></p></p><p><p>Import android.content.ContentProvider;</p></p><p><p>Import android.content.ContentUris;</p></p><p><p>Import android.content.ContentValues;</p></p><p><p>Import android.content.UriMatcher;</p></p><p><p>Import android.database.Cursor;</p></p><p><p>Import android.database.sqlite.SQLiteDatabase;</p></p><p><p>Import android.net.Uri;</p></p><p><p><br></p></p><p><p>public class Personcontentprovider extends ContentProvider {</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Private static final String authority = "com.itheima28.sqlitedemo.providers.PersonContentProvider";</p></p><p><p><span style="white-space:pre"></span>private static final int preson_insert_code = 0; <span style="white-space:pre"></span> URI Matching code for actions added by the person table</p></p><p><p><span style="white-space:pre"></span>private static final int person_delete_code = 1;</p></p><p><p><span style="white-space:pre"></span>private static final int person_update_code = 2;</p></p><p><p><span style="white-space:pre"></span>private static final int person_query_all_code = 3;</p></p><p><p><span style="white-space:pre"></span>private static final int person_query_item_code = 4;</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>private static Urimatcher urimatcher;</p></p><p><p><span style="white-space:pre"></span>Private Personsqliteopenhelper mopenhelper; <span style="white-space:pre"></span> Database helper Object For//person table</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>static {</p></p><p><p><span style="white-space:pre"></span>Urimatcher = new Urimatcher (urimatcher.no_match);</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Add some URIs (extension Numbers)</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Content://com.itheima28.sqlitedemo.providers.personcontentprovider/person/insert</p></p><p><p><span style="white-space:pre"></span>Urimatcher.adduri (authority, "person/insert", preson_insert_code);</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Content://com.itheima28.sqlitedemo.providers.personcontentprovider/person/delete</p></p><p><p><span style="white-space:pre"></span>Urimatcher.adduri (authority, "person/delete", person_delete_code);</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>Content://com.itheima28.sqlitedemo.providers.personcontentprovider/person/update</p></p><p><p><span style="white-space:pre"></span>Urimatcher.adduri (authority, "person/update", person_update_code);</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Content://com.itheima28.sqlitedemo.providers.personcontentprovider/person/queryall</p></p><p><p><span style="white-space:pre"></span>Urimatcher.adduri (authority, "person/queryall", person_query_all_code);</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>content://com.itheima28.sqlitedemo.providers.personcontentprovider/person/query/#</p></p><p><p><span style="white-space:pre"></span>Urimatcher.adduri (authority, "person/query/#", person_query_item_code);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>Public Boolean onCreate () {</p></p><p><p><span style="white-space:pre"></span>Mopenhelper = new Personsqliteopenhelper (getcontext ());</p></p><p><p><span style="white-space:pre"></span>Return true;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>Public Cursor Query (uri uri, string[] projection, String selection,</p></p><p><p><span style="white-space:pre"></span>string[] selectionargs, String SortOrder) {</p></p><p><p><span style="white-space:pre"></span>Sqlitedatabase db = Mopenhelper.getreadabledatabase ();</p></p><p><p><span style="white-space:pre"></span>Switch (urimatcher.match (URI)) {</p></p><p><p><span style="white-space:pre"></span>Case Person_query_all_code://query the URI of everyone</p></p><p><p><span style="white-space:pre"></span>If (db.isopen ()) {</p></p><p><p><span style="white-space:pre"></span>cursor cursor = db.query ("person", projection, selection, selectionargs, null, null, sortOrder);</p></p><p><p><span style="white-space:pre"></span>Return cursor;</p></p><p><p><span style="white-space:pre"></span>Db.close (); You cannot close a database when you return a cursor result set</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Break</p></p><p><p><span style="white-space:pre"></span>Case Person_query_item_code:<span style="white-space:pre"></span>//query for a single data, with an ID at the end of the URI</p></p><p><p><span style="white-space:pre"></span>If (db.isopen ()) {</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Long id = Contenturis.parseid (uri);</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>cursor cursor = db.query ("person", projection, "_id =?", new String[]{id + ""}, null, null, sortOrder);</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>Return cursor;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Break</p></p><p><p><span style="white-space:pre"></span>Default</p></p><p><p><span style="white-space:pre"></span>throw new IllegalArgumentException ("uri mismatch:" + uri);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Return null;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>Public String GetType (uri Uri) {</p></p><p><p><span style="white-space:pre"></span>Switch (urimatcher.match (URI)) {</p></p><p><p><span style="white-space:pre"></span>Case Person_query_all_code://return Multiple Mime-type</p></p><p><p><span style="white-space:pre"></span>Return "vnd.android.cursor.dir/person";</p></p><p><p><span style="white-space:pre"></span>Case Person_query_item_code://returns a single mime-type</p></p><p><p><span style="white-space:pre"></span>Return "vnd.android.cursor.item/person";</p></p><p><p><span style="white-space:pre"></span>Default</p></p><p><p><span style="white-space:pre"></span>Break</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Return null;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>Public URI Insert (uri uri, contentvalues values) {</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Switch (urimatcher.match (URI)) {</p></p><p><p><span style="white-space:pre"></span>Case Preson_insert_code:<span style="white-space:pre"></span>//add people to person table</p></p><p><p><span style="white-space:pre"></span>Sqlitedatabase db = Mopenhelper.getwritabledatabase ();</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>If (db.isopen ()) {</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Long id = db.insert ("person", null, values);</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>Db.close ();</p></p><p><p><span style="white-space:pre"></span></p></p><p><p><span style="white-space:pre"></span>return Contenturis.withappendedid (uri, id);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Break</p></p><p><p><span style="white-space:pre"></span>Default</p></p><p><p><span style="white-space:pre"></span>throw new IllegalArgumentException ("uri mismatch:" + uri);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Return null;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>public int Delete (uri uri, String selection, string[] Selectionargs) {</p></p><p><p><span style="white-space:pre"></span>Switch (urimatcher.match (URI)) {</p></p><p><p><span style="white-space:pre"></span>Case Person_delete_code:<span style="white-space:pre"></span>//actions to delete data in the person table</p></p><p><p><span style="white-space:pre"></span>Sqlitedatabase db = Mopenhelper.getwritabledatabase ();</p></p><p><p><span style="white-space:pre"></span>If (db.isopen ()) {</p></p><p><p><span style="white-space:pre"></span>int count = Db.delete ("person", selection, selectionargs);</p></p><p><p><span style="white-space:pre"></span>Db.close ();</p></p><p><p><span style="white-space:pre"></span>Return count;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Break</p></p><p><p><span style="white-space:pre"></span>Default</p></p><p><p><span style="white-space:pre"></span>throw new IllegalArgumentException ("uri mismatch:" + uri);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Return 0;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p><span style="white-space:pre"></span>@Override</p></p><p><p><span style="white-space:pre"></span>public int update (uri uri, contentvalues values, String selection,</p></p><p><p><span style="white-space:pre"></span>string[] Selectionargs) {</p></p><p><p><span style="white-space:pre"></span>Switch (urimatcher.match (URI)) {</p></p><p><p><span style="white-space:pre"></span>Case Person_update_code://update the Person table operation</p></p><p><p><span style="white-space:pre"></span>Sqlitedatabase db = Mopenhelper.getwritabledatabase ();</p></p><p><p><span style="white-space:pre"></span>If (db.isopen ()) {</p></p><p><p><span style="white-space:pre"></span>int count = db.update ("person", values, selection, selectionargs);</p></p><p><p><span style="white-space:pre"></span>Db.close ();</p></p><p><p><span style="white-space:pre"></span>Return count;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Break</p></p><p><p><span style="white-space:pre"></span>Default</p></p><p><p><span style="white-space:pre"></span>throw new IllegalArgumentException ("uri mismatch:" + uri);</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><span style="white-space:pre"></span>Return 0;</p></p><p><p><span style="white-space:pre"></span>}</p></p><p><p><br></p></p><p><p>}</p></p><p><p><br></p></p><p><p>Learn from Android under ContentProvider</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.