Android uses Contentresolver to search for a phone address book _android

Source: Internet
Author: User
Tags stub

This article illustrates how Android uses Contentresolver to search for a phone address book. Share to everyone for your reference, specific as follows:

Use Contentresolver in this program to access the Contacts keyword in your address book and deposit all the contacts you find in CursorAdapter. Enter the name of the search person A, will all start with a name will be displayed, input *, then all the names in the address book displayed in the Autocompleteview Adapterview, if the user selection event occurred, The checked contact number will be displayed in the TextView.

This program uses Contentresolver. Query (URI URI, string[] projection, string selection, string[] Selectionargs, String sortor Der) to remove all contacts in the Address Book, where the selection and Selectionargs are passed in to the null rep to find all of them. Using Cursor's getstring (column index) to get the storage content, where column index starts at 0, this differs from Java.sql.ResultSet because it starts at 1. With the Autocompletetextview.onitemclicklistener event, which is the event handling that is intercepted after the user clicks the contact, in which the contact's phone number is obtained in the Contactsadapter.getcursor () method.

The procedure is as follows:

Import android.app.Activity;
Import Android.content.ContentResolver;
Import Android.database.Cursor;
Import Android.os.Bundle;
Import android.provider.Contacts;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.AutoCompleteTextView;
Import Android.widget.TextView;
 @SuppressWarnings ("deprecation") public class A07activity extends activity {private Autocompletetextview ACTV;
 Private TextView TV;
 Private Cursor C01;
 Private Contactsadapter CA; Locate the fields in the Address Book public static string[] people={contacts.people._id, Contacts.People.PRIMARY_PHONE_ID, Contacts.people. 
  TYPE, Contacts.People.NUMBER, Contacts.People.LABEL, Contacts.People.NAME}; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
  actv= (Autocompletetextview) Findviewbyid (R.ID.ACTV);  tv= (TextView) Findviewbyid (r.id.tv);
    Contentresolver Cr=getcontentresolver ();
    C01=cr.query (Contacts.People.CONTENT_URI, people, NULL, NULL, Contacts.People.DEFAULT_SORT_ORDER);
    Ca=new Contactsadapter (THIS,C01);
    Actv.setadapter (CA); Actv.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> arg0, V
  Iew arg1, int arg2, long arg3) {//TODO auto-generated method Stub Cursor c02=ca.getcursor ();
  C02.movetoposition (ARG2);
  String number=c02.getstring (C02.getcolumnindexorthrow (Contacts.People.NUMBER)); Number=number==null? "
  No phone input ": number;
  Tv.settext (C02.getstring (C02.getcolumnindexorthrow (Contacts.People.NAME)) + "Tel is:" +number);
  }
    });

 }
}
Import Android.content.ContentResolver;
Import Android.content.Context;
Import Android.database.Cursor;
Import android.provider.Contacts;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.CursorAdapter;
Import Android.widget.TextView;
 @SuppressWarnings ("deprecation") public class Contactsadapter extends cursoradapter{contentresolver CR;
 Public Contactsadapter (context, Cursor c) {Super (context, c);
 TODO auto-generated Constructor stub cr=context.getcontentresolver (); @Override public void BindView (view view, context, Cursor Cursor) {//TODO auto-generated the method stub (Tex
 Tview view). SetText (Cursor.getstring (Cursor.getcolumnindexorthrow (Contacts.People.NAME)));
 @Override Public View Newview (context context, Cursor Cursor, ViewGroup parent) {//TODO auto-generated method stub
 Final Layoutinflater li=layoutinflater.from (context); Final TextView tv= (TextView) li.inflate (Android. R.layOut.simple_dropdown_item_1line, parent, false);
 Tv.settext (Cursor.getstring (Cursor.getcolumnindexorthrow (Contacts.People.NAME)));
 return TV;
 Public String convertostring (Cursor c) {return c.getstring (C.getcolumnindexorthrow (Contacts.People.NAME)); @SuppressWarnings ("null") public Cursor Runqueryonbackgroundthread (Charsequence cs) {if (Getfilterqueryprovider ()!=
 NULL) {Getfilterqueryprovider (). RunQuery (CS);
 } StringBuilder Sb=null;
 String[] S=null;
  if (cs==null) {sb=new StringBuilder ();
  Sb.append ("UPPER (");
  Sb.append (Contacts.ContactMethods.NAME);
   Sb.append (") GLOB");
 S=new string[]{cs.tostring (). toUpperCase () + "*"}; 
     Return Cr.query (Contacts.People.CONTENT_URI, A07activity.people, sb==null? null:sb.toString (), S,
 Contacts.People.DEFAULT_SORT_ORDER);

 }
}

The Androidmanifest.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
  package=" com.my.a07 "
  android:versioncode=" 1 "
  android:versionname=" 1.0 ">
  < USES-SDK android:minsdkversion= "Ten"/>
  <application
    android:icon= "@drawable/ic_launcher"
    Android:label= "@string/app_name" >
    <activity
      android:name= ". A07activity "
      android:label=" @string/app_name ">
      <intent-filter>
        <action android:name= "Android.intent.action.MAIN"/>
        <category android:name= "Android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
  </application>
  <uses-permission android:name= " Android.permission.READ_CONTACTS "></uses-permission>
</manifest>

Contentresolver also allows you to add, modify, and delete information from your address book, and access audio, video, images, and more. The corresponding method is shown as follows:

Add: Public final Uri Insert (URI uri,contentvalues values), Contentvalue.put (Key,value), where key is the field name and value is the added data.

Modified: Public final int update (Uri uri,contentvalues values,string where, string[] Selectionargs), where is the condition string following the SQL where. Selectionargs is the data in the where.

Delete: public final int Delete (Uri uri,string where,string[] selectionargs).

The Contentvalue.put () method is used below. Add the contact person's information in the Address book through the program.

More interested readers of Android-related content can view this site: "The summary of Android controls usage" and "Android Development introduction and Advanced Course"

I hope this article will help you with the Android program.

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.