"Android" uses Autocompletetextview control contact Auto-complete and query phone based on contact name

Source: Internet
Author: User
Tags sqlite database contact form
<span id="Label3"></p><p><p>Auto-completion is one of the more user-friendly features of the app, but it's not easy to use the Autocompletetextview Auto-fill TextBox control because it involves an adapter that autocompletetextview the Data. With the Autocompletetextview listener. Also need to use Contentresolver to find the Device's address book, of course, and "android" using the Android data interface, multimedia processing memory card MP3 Player app (click The open link) to traverse MP3 and "android" SQLite database Additions and deletions (click The open link) to traverse the SQLite database is the Same. Here is a small example of how Autocompletetextview is Used:</p></p><p><p><br></p></p><p><p>For example, If there are 3 contacts in the User's phone, there is now a text box that can be automatically completed, enter the name, and automatically display its phone number in the address Book. If not, give the appropriate Information.</p></p><p><p>The production process is as Follows:</p></p><p><p>1, First Res\values\strings.xml font files, nothing to say:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"?><resources> <string name= "app_name" > Auto complement contacts and find their phone < /string> <string name= "action_settings" >Settings</string> <string name= "textView1" > Name:</string></resources></pre></pre><br>2, after the layout changes in the Res\layout\activity_main.xml. A horizontal linear layout is placed in the middle of a vertical linear layout so that the TextView1 and AutoComplete text boxes are in one line. After a display of the results of the TEXTVIEW2 assigned to the id, where the AutoCompleteTextView1 setting android:completionthreshold= "1", refers to the user input 1 characters to start giving complete information.<p><p></p></p><p><p></p></p><pre name="code" class="html"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <linearlayout Android:layout_widt h= "match_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" > <TextV Iew android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text = "@string/textview1" android:textsize= "24sp"/> <autocompletetextview android:id= "@+id/ AutoCompleteTextView1 "android:layout_width=" match_parent "android:layout_height=" wrap_content " android:completionthreshold= "1"/> </LinearLayout> <textview android:id= "@+id/textview2" Android:layout_width= "match_parent" android:layout_height= "wrap_content" android:textsize= "24sp"/> </LinearLayout></pre><br>3. Request permission to read contacts in Androidmanifest.xml.<p><p></p></p><p><p></p></p><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" com.autocomplete "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" Android.permiss Ion. Read_contacts "/><!--require Read Contacts--<application android:allowbackup=" true "android:icon=" @draw Able/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity Android:name= "com.autocomplete.MainActivity" android:label= "@string/app_name" > <in tent-filter> <action android:name= "android.intent.action.MAIN"/> <category Android Oid:name= "android.intent.category.LAUNCHER"/> </intent-filter> </activity> </appli Cation></manifest></pre><br>4, Mainactivity.java is the focus of this app, which contains Autocompletetextview automatic text box adapter, used to fill the Data. The data is the name information found in the contact form on the Android device through Contentresolver. After that, the mainactivity itself implements the Textwatcher interface, which is used as the listener for the Autocompletetextview automatic text box, and the current user selects the option of auto-completion or their own input data, which will trigger Textwatcher Monitoring. According to the contact person to query the phone, it is necessary to both the wirelessly intrinsic two tables, one is the user device contact name table, One is the contact name table with the ID of the contact phone List. The specific code is as Follows:<p><p></p></p><p><p></p></p><pre name="code" class="java">Package Com.autocomplete;import Android.net.uri;import Android.os.bundle;import Android.provider.contactscontract.commondatakinds.phone;import android.provider.ContactsContract.Contacts; Import Android.support.v4.widget.cursoradapter;import Android.text.editable;import android.text.TextWatcher; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.autocompletetextview;import Android.widget.filterqueryprovider;import android.widget.Filterable; Import Android.widget.textview;import Android.app.activity;import Android.content.contentresolver;import Android.content.context;import Android.database.cursor;//autocompletetextview Automatic text box adapter class ContactListAdapter Extends CursorAdapter implements filterable {private contentresolver resolver;private string[] columns = new string[] {Co Ntacts._id,contacts.display_name};//for a moment, the cursor queries out the table on two columns, one column is the ID, the other is the contact name public Contactlistadapter (context context, Cursor c, int Flags) {//call The parent class construction method, here ContactlistadApter (context context, Cursor//c) method is obsolete, a more than one parameter flags//actually no use, flags throw a 0 to it on the Oksuper (Context, c, flags); resolver = Context.getcontentresolver ();//initialize contentresolver}//here to form a drop-down menu @overridepublic void BindView (view view, Context arg1, cursor cursor) {(TextView) view. setText (cursor.getstring (1));} @Overridepublic View newview (context context, cursor cursor, viewgroup parent) {layoutinflater inflater = layoutinflater. From (context); TextView view = (TextView) inflater.inflate (android. r.layout.simple_dropdown_item_1line, parent, false); view.settext (cursor.getstring (1));//the text of each item is the cursor query, Contact name that column return view;} @Overridepublic charsequence converttostring (cursor cursor) {return cursor.getstring (1);} @Overridepublic Cursor runqueryonbackgroundthread (charsequence constraint) {filterqueryprovider filter = Getfilterqueryprovider (); If (filter! = Null) {return filter.runquery (constraint);} Here is the table indicating the cursor query, with the query result column URI uri = Uri.withappendedpath (contacts.content_filter_uri,uri.encode (constraint.tostrinG ())); return resolver.query (uri, columns, null, null, null);}} public class Mainactivity extends Activity implements Textwatcher {private Autocompletetextview autoCompleteTextView1; Private TextView textView2; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate ( savedinstancestate); Setcontentview (r.layout.activity_main);//register Component autoCompleteTextView1 = (autocompletetextview) Findviewbyid (r.id.autocompletetextview1); textView2 = (TextView) Findviewbyid (r.id.textview2);// Constructs the Autocompletetextview adapter contentresolver contentresolver = Getcontentresolver (); string[] colmuns = new string[] {contacts._id, contacts.display_name}; cursor cursor = contentresolver.query (contacts.content_uri, colmuns,null, null, null); Contactlistadapter contactlistadapter = new Contactlistadapter (this,cursor, 0);// Indicates the Autocompletetextview adapter and listener Autocompletetextview1.setadapter (contactlistadapter); Autocompletetextview1.addtextchangedlistener (this);} Query the phone according to the name of the contact private string Getquerydata (string naMe) {String phonenum = "";//check Contact Name This table, there is no user input name Contentresolver contentResolver1 = Getcontentresolver (); string[] colmuns1 = new string[] {contacts._id, contacts.display_name}; Cursor Cursor1 = contentresolver1.query (contacts.content_uri, null,null, null, null); for (cursor1.movetofirst ();! ( Cursor1.isafterlast ()); Cursor1.movetonext ()) {String contactsname = cursor1.getstring (cursor1.getcolumnindex (colmuns1[1]));//if there is, Start checking the contact phone number This form if (name.equals (contactsname)) {int id = cursor1.getint (cursor1.getcolumnindex (colmuns1[0])); Contentresolver ContentResolver2 = Getcontentresolver (); string[] colmuns2 = new string[] {phone.contact_id,phone.number};//query The contact phone This table, add a condition, check the directory contact name in the column data id// Because the contact phone this table and contact name This table is the same id//query parameter placed in the third parameter this position cursor Cursor2 = contentresolver2.query (phone.content_uri,null, colmuns2[ 0] + "=" + id, null, null), if (cursor2.movetonext ()) {///if there is data in the cursor, the cursor with the initial position of 0 must be pulled down to get the data Phonenum = Cursor2.getstri ng (cursor2.getcolumnindex (colmuns2[1));}}} return Phonenum;//find the correspondingThe phone, did not find to throw back 0}//autocompletetextview automatic text box listener, decisive is waiting for information to complete the whole place to the text box is to find @overridepublic void aftertextchanged (Editable Arg0) {String name = Autocompletetextview1.gettext () + ""; String Phonenum = getquerydata (name), if (phonenum.equals ("")) {textview2.settext ("no Contact!") ");} else {textview2.settext ("phone is:" + phonenum);}} @Overridepublic void beforetextchanged (charsequence arg0, int arg1, int Arg2,int arg3) {//TODO auto-generated Method stub } @Overridepublic void ontextchanged (charsequence arg0, int arg1, int arg2, int arg3) {//TODO auto-generated method stub}}</pre><br><br><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>"Android" uses Autocompletetextview control contact Auto-complete and query phone based on contact name</p></p></span>

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.