Android Development (7) database and Content Provider

Source: Internet
Author: User


Problem focus:
Idea: Application Data Sharing
Access to the database is limited to the application that creates it, but it is not absolute.
Content Provider provides a standard interface for other applications to access and use data of other programs.

Next we will analyze the ContentProvider processing process from the bottom up:
1 database -- SQLite User: ContentProvider ing: Content Value ing is a row of the table in the database; query: Cursor, pointer to the result set in the underlying data; Use: SQLiteOpenHelper, abstract class, used to create, open, and upgrade Databases
Inherit this class, override its constructor, onCreate method (create) and onUpgrade method (update)
Access: getWritableDatabase (obtain writable database instances) and getReadableDatabase (obtain read-only database instances)
Demo
/** Insert a new row ** // create the new value of the row to be inserted ContentValues newValues = new ContentValues (); // assign newValues to each field. put (NAME_COLUMN, _ name); newValues. put (ADDRESS_COLUMN, _ address );...... // insert it into the database SQLiteDatabase db = DBOpenHelper. getWritableDatabase (); db. insert (DBOpenHelper. DATABASE_TABLE, null, newValues );

2 Content Provider User: Independent APP function: separates the application component using data from the underlying data source using ContentProvider, abstract class, inheriting this class, and rewriting method:
OnCreate (): Initialize the underlying data source query () update () delete () insert () getType ()
Registration: Implemented by TAG, name Attribute + authorities tag
Demo:
    
     
  
Release: URI demo:
            public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/item");            public static final Uri CONTENT_POS_URI = Uri.parse("content://" + AUTHORITY + "/pos");
Usage: The application interacts with ContentProvider through ContentResolver
3 ContentResolver -- use ContentProvider User: Application role: the application uses it to query and operate Content Provdier features: ContentProvider provides the abstraction of underlying data
ContentResolver provides abstraction for querying and processing ContentProvider
Use: Use URI to interact with the Demo
/** Query operation **/public Article getArticleById (int id) {Uri uri = ContentUris. withAppendedId (Articles. CONTENT_URI, id); String [] projection = new String [] {Articles. ID, Articles. TITLE, Articles. ABSTRACT, Articles. URL}; Cursor cursor = resolver. query (uri, projection, null, null, Articles. DEFAULT_SORT_ORDER); // obtain the cursor Log of a database. I (LOG_TAG, "cursor. moveToFirst "); if (! Cursor. moveToFirst () {return null;} String title = cursor. getString (1); String abs = cursor. getString (2); String url = cursor. getString (3); return new Article (id, title, abs, url );}

4 URIUri indicates the data format to be operated: content: // The Host Name (or Authority) is used to uniquely identify the ContentProvider. External callers can find it based on this identifier. Path can be used to indicate the data to be operated.
5. Summary: A Picture summarizes the content of this chapter:
Programming 3rd ContentProvider and Uri detailed explanation: http://www.cnblogs.com/linjiqin/archive/2011/05/28/2061396.html

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