Contentprovider data sharing on the sixth day of Android Development

Source: Internet
Author: User


Public class provider extends contentprovider {public final string tag = "provider";/*** this parameter must be used with manifest. consistent in XML */public static final string authority = "com. nanguabing. contentprovider. myprovider "; Private Static urimatcher matcher = new urimatcher (urimatcher. no_match); static {try {matcher. adduri (authority, "insert", 1); matcher. adduri (authority, "delete/#", 2); matcher. adduri (authority, "Update /# ", 3); matcher. adduri (authority, "query/#", 4);} catch (exception e) {e. printstacktrace () ;}} crud mcrud; @ override public Boolean oncreate () {mcrud = new CRUD (this. getcontext (); Return false;}/*** query operation */Public cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder) {int code = matcher. match (URI); If (code = 4) {return mcrud. findall ();} Return NULL;} @ override Public String GetType (URI) {// todo auto-generated method stub return NULL ;} /*** insert operation */Public URI insert (URI Uri, contentvalues values) {// URI = content: // COM. nanguabing. contentprovider. myprovider/Insert int code = matcher. match (URI); long id =-1; if (code = 1) {mcrud. insert (values. getasstring ("name"), values. getasinteger ("Age"); Id = 1;} else {log. I (TAG, "t His URI is invalid for insert! ");} Return contenturis. withappendedid (Uri, ID);}/*** delete operation */Public int Delete (URI Uri, string selection, string [] selectionargs) {// Delete: uri = content: // CN. itcast. provider. customerprovider/delete/10 int code = matcher. match (URI); If (code = 2) {mcrud. delete (INT) contenturis. parseid (URI);} else {log. I (TAG, "This URI is invalid for Delete! ");} Return 0;}/*** modify operation */Public int Update (URI Uri, contentvalues values, string selection, string [] selectionargs) {// update: uri = content: // CN. itcast. provider. customerprovider/update/10 int code = matcher. match (URI); If (code = 3) {mcrud. update (values. getasstring ("name"), values. getasinteger ("Age"), values. getasinteger ("ID");} else {log. I (TAG, "This URI is invalid for update! ") ;}Return 0 ;}}

Mainfest. xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android=""    package="com.nanguabing.contentprovider"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="8" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.nanguabing.contentprovider.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <provider android:name="com.nanguabing.contentprovider.provider"                android:authorities="com.nanguabing.contentprovider.myprovider"/>    </application></manifest>


Public class mainactivity extends activity {string uri = ""; textview MTV; edittext mid, mname, Mage; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); MTV = (textview) findviewbyid (R. id. textview1); Mid = (edittext) findviewbyid (R. id. edittext1); mname = (edittext) findviewbyid (R. id. edittext2); mage = (edittext) findviewbyid (R. id. edittext3);} public void onclick (view v) {contentresolver Cr = This. getcontentresolver (); contentvalues values = new contentvalues (); Switch (v. GETID () {case R. id. button1: uri = "content: // COM. nanguabing. contentprovider. myprovider/Insert "; values. put ("name", mname. gettext (). tostring (); values. put ("Age", integer. parseint (mage. gettext (). tostring (); CR. insert (URI. parse (URI), values); break; case R. id. button2: uri = "content: // COM. nanguabing. contentprovider. myprovider/delete/"+ mid. gettext (). tostring (); CR. delete (URI. parse (URI), null, null); break; case R. id. button3: uri = "content: // COM. nanguabing. contentprovider. myprovider/update/"+ mid. gettext (). tostring (); values. put ("ID", integer. parseint (MID. gettext (). tostring (); values. put ("name", mname. gettext (). tostring (); values. put ("Age", integer. parseint (mage. gettext (). tostring (); CR. update (URI. parse (URI), values, null, null); break; case R. id. button4: uri = "content: // COM. nanguabing. contentprovider. myprovider/query/1 "; cursor cur = CR. query (URI. parse (URI), null); string TXT = ""; while (cur. movetonext () {TXT + = cur. getint (cur. getcolumnindex ("ID") + cur. getstring (cur. getcolumnindex ("name") + cur. getint (cur. getcolumnindex ("Age") + "\ n";} MTV. settext (txt); // close the cursor cur. close (); break; default: break; }}@ override public Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

Mobile development QQ group: 59516399

Csdn download link:

Related Article

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: 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.