Android: Control Autocompletetextview Client Save search History auto Prompt

Source: Internet
Author: User

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" >    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" >        <AutocompletetextviewAndroid:id= "@+id/auto"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:completionhint= "Last 5 records"Android:completionthreshold= "1"            />        <ButtonAndroid:id= "@+id/search"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Search" />    </LinearLayout>    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" >        <ButtonAndroid:id= "@+id/clean"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Clear History"Android:onclick= "Cleanhistory"            />    </LinearLayout></LinearLayout>

 Public classTestactivityextendsActivity {PrivateAutocompletetextview Auto; PrivateButton searchbtn; PrivateArrayadapter<string>Arr_adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.test); //InitializeAuto =(Autocompletetextview) Findviewbyid (R.id.auto); SEARCHBTN=(Button) Findviewbyid (R.id.search); //Get search Record file contentsSharedpreferences sp = getsharedpreferences ("Search_history", 0); String History= Sp.getstring ("History", "No search record temporarily"); //returns an array with comma-delimited contentstring[] History_arr = History.split (","); //new adapter, adapter data for Search history file contentArr_adapter =NewArrayadapter<string> ( This, Android.        R.layout.simple_dropdown_item_1line, History_arr); //retain the first 50 data        if(History_arr.length > 50) {string[] newarrays=NewString[50]; //to implement replication between arraysSystem.arraycopy (History_arr, 0, newarrays, 0, 50); Arr_adapter=NewArrayadapter<string> ( This, Android.        R.layout.simple_dropdown_item_1line, History_arr); }        //setting up the adapterAuto.setadapter (Arr_adapter); //Set Listener events, click Search to write search termsSearchbtn.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSave ();    }        }); }     Public voidSave () {//Get search box informationString Text =Auto.gettext (). toString (); Sharedpreferences MYSP= Getsharedpreferences ("Search_history", 0); String old_text= Mysp.getstring ("History", "No search record temporarily"); //use Stringbuilder.append to add content, comma to easily read content when separated by commasStringBuilder Builder =NewStringBuilder (old_text); Builder.append (Text+ ","); //determines whether the search content already exists in the history file, and does not repeat the addition        if(!old_text.contains (text + ",") {sharedpreferences.editor Myeditor=Mysp.edit (); Myeditor.putstring ("History", builder.tostring ());            Myeditor.commit (); Toast.maketext ( This, text + "Add Success", Toast.length_short). Show (); } Else{Toast.maketext ( This, text + "already exists", Toast.length_short). Show (); }    }        //Clear Search History     Public voidcleanhistory (View v) {sharedpreferences SP=getsharedpreferences ("Search_history", 0); Sharedpreferences.editor Editor=Sp.edit ();        Editor.clear ();        Editor.commit (); Toast.maketext ( This, "clear Success", Toast.length_short). Show (); Super. OnDestroy (); }    }

Example Download >>>>

Related articles:

Sharepreferences of storage methods

Autocompletetextview Automatic Tips

Android: Control Autocompletetextview Client Save search History auto Prompt

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.