ContentProvider on + Example

Source: Internet
Author: User

As one of the four components of Android, ContentProvider is also useful, and ContentProvider is used to save and retrieve data, which is an interface for sharing data between different applications in the Android system.

The most intuitive application is when you send a text message that requires information about the contact, and you access the phone book on the Android system via the interface provided by ContentProvider, and select the contact from it.

The Android system provides a corresponding ContentProvider interface for a range of common common data types, all defined under the Android.provider package.

ContentProvider enables shared data: ContentProvider provides a set of interfaces between applications that can be accessed through ContentProvider to share data from the current application to other applications. Two other applications can also access the specified application through ContentProvider. There are 2 ways to expose your data to other applications, 1. Define your own ContentProvider subclass, 2. Add the currently applied data to the existing contentprovider.


Here's an example of using ContentProvider to access your phone's contacts.




Mainactivity Code:


public class Mainactivity extends Activity {


Private string[] columns={contacts._id,

Contacts.display_name,

Phone.number,

phone.contact_id

};

Private ListView ListView;

Private Dialog Dialog;

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

listview= (ListView) Findviewbyid (R.ID.LIST1);

Dialog=new dialog (mainactivity.this);

Dialog.settitle ("Telephone");

Dialog.setcontentview (R.layout.content_dialog);

Dialog.setcanceledontouchoutside (TRUE);

Showinfo ();

Listview.setonitemclicklistener (New Onitemclicklistener () {


public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,

Long Arg3) {

TODO auto-generated Method Stub

Dialog.show ();

}

});

}

private void Showinfo () {

Arraylist

Contentresolver Resolver=getcontentresolver ();

Cursor cursor=resolver.query (Contacts.content_uri, null,null,null,null);

while (Cursor.movetonext ()) {

hashmap<string,string> map=new hashmap<string, string> ();

String liststr= "";

int Idindex=cursor.getcolumnindex (columns[0]);

int Nameindex=cursor.getcolumnindex (columns[1]);

int Id=cursor.getint (IDINDEX);

String name=cursor.getstring (Nameindex);

Cursor phone=resolver.query (Phone.content_uri, null,columns[3]+ "=" +id,null,null);

while (Phone.movetonext ()) {

int Phonenumberindex=phone.getcolumnindex (columns[2]);

String phonenumber=phone.getstring (Phonenumberindex);

Liststr+=phonenumber;

}

map.put ("name", name);

map.put ("PhoneNumber", liststr);

list.add (map);

}

Cursor.close ();

Simpleadapter simpleadapter=new Simpleadapter (this, list, R.layout.simple_adapter_item, new string[]{"name", " PhoneNumber "}, New Int[]{r.id.row_text1,r.id.row_text2});

Listview.setadapter (Simpleadapter);

}



}


In this main code, dialog is a hover dialog box that appears with a click of a contact, the red part.


The contact list is primarily the Showinfo () method, which is the blue part, with the Simpleadapter adaptation in the ListView.



The corresponding XML file is relatively simple and is given here:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/75/4F/wKioL1Y2ttXC_VV5AAA8E0MwpUI247.jpg "title=" 2015-11-02 09-02-31.jpg "alt=" Wkiol1y2ttxc_vv5aaa8e0mwpui247.jpg "/>




Activity_main.xml:


<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"

Xmlns:tools= "Http://schemas.android.com/tools"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

Tools:context= ". Mainactivity ">


<listview

Android:id= "@+id/list1"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

Android:scrollbaralwaysdrawverticaltrack= "true" >

</ListView>


</RelativeLayout>




Content_dialog.xml:


<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

android:orientation= "Vertical" >

<textview

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "--Phone number--"/>


</LinearLayout>




Simple_adapter_item.xml:


<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

android:orientation= "Vertical" >

<textview

Android:id= "@+id/row_text1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "--------"

/>

<textview

Android:id= "@+id/row_text2"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "--------"

/>


</LinearLayout>





At the same time, permission settings are required, in Androidmainfest.xml,


<uses-permission android:name= "Android.permission.READ_CONTACTS"/>




This will be OK, you can access the system's phone book.

This article is from the "Software Learning Summary" blog, be sure to keep this source http://bigcrab.blog.51cto.com/10626858/1708690

ContentProvider on + Example

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.