--------------Main.java-----------------
Package COM.EXAMPLE.VD;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Android.content.ContentResolver;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.support.v7.app.ActionBarActivity;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
public class Mainactivity extends Actionbaractivity {
Private ListView list_select_contact;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
List_select_contact = (ListView) Findviewbyid (r.id.list_select_contact);
Final list<map<string, string>> data = Getcontactinfo ();
List_select_contact.setadapter (New Simpleadapter (this, data,
R.layout.line, new string[] {"Name", "Phone", "others"},
New int[] {r.id.tv_name, r.id.tv_phone, r.id.tv_other});
}
/**
* Read the contacts inside the hand
*
* @return
*/
Private list<map<string, string>> Getcontactinfo () {
Put all the contacts
list<map<string, string>> list = new arraylist<map<string,string>> ();
Get a Content Parser
Contentresolver resolver = Getcontentresolver ();
Raw_contacts URI
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");
Uri uridata = Uri.parse ("Content://com.android.contacts/data");
cursor cursor = resolver.query (URI, new string[] {"contact_id"},
NULL, NULL, NULL);
while (Cursor.movetonext ()) {
String contact_id = cursor.getstring (0);
if (contact_id! = null) {
Specific one of the contacts
map<string, string> map = new hashmap<string, string> ();
Cursor datacursor = Resolver.query (Uridata, new string[] {
"Data1", "MimeType"}, "Contact_id=?",
New string[] {contact_id}, NULL);
while (Datacursor.movetonext ()) {
String data1 = datacursor.getstring (0);
String mimetype = datacursor.getstring (1);
System.out.println ("data1==" +data1+ "==mimetype==" +mimetype);
if ("Vnd.android.cursor.item/name". Equals (MimeType)) {
Name of contact person
Map.put ("name", data1);
}else if ("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {
Phone number of the contact person
Map.put ("Phone", data1);
} else if ("Vnd.android.cursor.item/note". Equals (MimeType)) {
Map.put ("Others", data1);
}
}
List.add (map);
Datacursor.close ();
}
}
Cursor.close ();
return list;
}
}
。。。。。。。。 Main.xml .....
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<listview
Android:id= "@+id/list_select_contact"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:layout_margintop= "15dip"
android:verticalspacing= "10dip"/>
</LinearLayout>
。。。。。。 /layout/line.xml .....
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >
<textview
Android:id= "@+id/tv_name"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textcolor= "#ff00ff"
/>
<textview
Android:id= "@+id/tv_phone"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textcolor= "#ff00ff"
/>
<textview
Android:id= "@+id/tv_other"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textcolor= "#ff00ff"
/>
</LinearLayout>
Permissions
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
~~~~~~~~~~~ Additional Instructions ~~~~~~~~~
If you want to increase the ability to add contacts, refer to the following methods
public void Addcontacts (View v) {
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");
Uri Datauri = Uri.parse ("Content://com.android.contacts/data");
1. Add a record to the Raw_contacts table
Take the value of contact_id in the Raw_contacts table
cursor cursor = getcontentresolver (). Query (URI, new string[]{"contact_id"}, NULL, NULL, "contact_id desc limit 1");
if (cursor! = NULL && Cursor.movetofirst ()) {
int contact_id = cursor.getint (0);
contact_id + +;
Cursor.close ();
Contentvalues values = new Contentvalues ();
Values.put ("contact_id", contact_id);
Getcontentresolver (). Insert (URI, values);
2. Based on the ID of the record added above, add three data to the table.
Deposit Number
values = new Contentvalues ();
Values.put ("MimeType", "vnd.android.cursor.item/phone_v2");
Values.put ("Data1", "10086");
Values.put ("raw_contact_id", contact_id);
Getcontentresolver (). Insert (Datauri, values);
Save Name
values = new Contentvalues ();
Values.put ("MimeType", "vnd.android.cursor.item/name");
Values.put ("Data1", "China Mobile");
Values.put ("raw_contact_id", contact_id);
Getcontentresolver (). Insert (Datauri, values);
Save Mail
values = new Contentvalues ();
Values.put ("MimeType", "vnd.android.cursor.item/email_v2");
Values.put ("Data1", "[email protected]");
Values.put ("raw_contact_id", contact_id);
Getcontentresolver (). Insert (Datauri, values);
}
}
1 reading, adding contacts