Android Learning note _79_ Android use search box

Source: Internet
Author: User

1. Create an XML folder under the Resources folder and create a searchable.xml:

<?XML version= "1.0" encoding= "Utf-8"?><searchablexmlns:android= "Http://schemas.android.com/apk/res/android"Android:label= "@string/sms_search"Android:hint= "@string/sms_search"android:searchsuggestauthority= "Cn.itcast.smsmanager.MySuggestionProvider"android:searchsuggestselection=" ?" ></searchable>

2. Configuration files

<ActivityAndroid:name=". Searchableactivity "Android:theme= "@android: Style/theme.notitlebar" >            <!--The following two nodes must have -            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.SEARCH" />            </Intent-filter>            <Meta-dataAndroid:name= "Android.app.searchable"Android:resource= "@xml/searchable" />        </Activity>        <!--Specify which searchableactivity we want to activate. -        <Meta-dataAndroid:name= "Android.app.default_searchable"Android:value=". Searchableactivity " />

3. Create Provider

 Public classMysuggestionproviderextendsSearchrecentsuggestionsprovider { Public Final StaticString authority = "Cn.itcast.smsmanager.MySuggestionProvider";  Public Final Static intMODE =database_mode_queries; Private Final Staticstring[] Sms_projection =NewString[]{sms._id,sms.address,sms.body}; Private Final Staticstring[] ColumnNames =Newstring[]{basecolumns._id, searchmanager.suggest_column_text_1, Searchmanager.suggest_column_text_2,    Searchmanager.suggest_column_query};  PublicMysuggestionprovider () {setupsuggestions (authority, MODE); } @Override PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { //TODO auto-generated Method StubLOG.I ("i", "Query ..."); if(Selectionargs! =NULL) {String query= Selectionargs[0]; if(textutils.isempty (query)) {return NULL; } Uri Uri1=Sms.content_uri; String where= Sms.body + "like '%" + query + "% '"; Cursor Cursor= GetContext (). Getcontentresolver (). Query (Uri1, Sms_projection, where,NULL, sms.date + "desc"); returnchangecursor (cursor); }        return NULL; }            Privatecursor changecursor (cursor cursor) {matrixcursor result=Newmatrixcursor (ColumnNames); if(Cursor! =NULL){             while(Cursor.movetonext ()) {object[] columnvalues=Newobject[]{cursor.getstring (Cursor.getcolumnindex (sms._id)), cursor.getstring (cursor.getcolumnin Dex (sms.address)), Cursor.getstring (Cursor.getcolumnindex (sms.body)), cursor                . GetString (Cursor.getcolumnindex (sms.body))};            Result.addrow (columnvalues); }        }        returnresult; }}

4. Activity to store search results:

 Public classSearchableactivityextendslistactivity {PrivateListView Mlistview; PrivateQueryhandler Mqueryhandler; PrivateSearchadapter Madapter; protected voidonCreate (Android.os.Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Intent Intent=getintent (); Mlistview=Getlistview (); Madapter=NewSearchadapter ( This,NULL);                Mlistview.setadapter (Madapter);                Mlistview.setbackgroundcolor (Color.White); if(Intent.ACTION_SEARCH.equals (Intent.getaction ())) {String query=Intent.getstringextra (searchmanager.query);                  Dosearch (query); } mlistview.setonitemclicklistener (NewMyonitemclicklistener ()); }}

Http://www.cnblogs.com/zhengbeibei/archive/2013/01/17/2865610.html

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.