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"> <button android:id= "@+id/clean"  Android:layout_width= "Match_parent"  Android:layout_height= "Wrap_ Content " Android:text Android:onclick=" Cleanhistory "/> </linearlayout ></ Linearlayout>          

PublicClass TestactivityExtendsActivity {PrivateAutocompletetextview Auto;PrivateButton searchbtn;Private arrayadapter<string>Arr_adapter; @OverrideProtectedvoidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.test);//Initialize auto =(Autocompletetextview) Findviewbyid (R.id.auto); SEARCHBTN =(Button) Findviewbyid (R.id.search);//Get Search Record file contents Sharedpreferences SP = getsharedpreferences ("Search_history", 0); String history = sp.getstring ("Historical", "No search record temporarily");//Returns an array of contents with a comma string[] History_arr = History.split (",");//New adapter, adapter data for Search history file Content Arr_adapter =New Arrayadapter<string> (ThisAndroid. R.layout.simple_dropdown_item_1line, History_arr);//Retain the first 50 dataif (History_arr.length > 50) {string[] newarrays =New STRING[50];//Implementing replication between Arrays System.arraycopy (History_arr, 0, newarrays, 0, 50); Arr_adapter =New Arrayadapter<string> (ThisAndroid. R.layout.simple_dropdown_item_1line, History_arr); }//Setting up the adapterAuto.setadapter (Arr_adapter);//To set the Listener event, click Search to write the search term searchbtn.setonclicklistener (NewButton.onclicklistener () {@OverridePublicvoidOnClick (View v) {//TODO auto-generated Method StubSave (); } }); }PublicvoidSave () {//Get search box information String text =Auto.gettext (). toString (); Sharedpreferences MYSP = getsharedpreferences ("Search_history", 0); String old_text = mysp.getstring ("History", "No search record temporarily");//With stringbuilder.append additions, comma-separated StringBuilder builder = comma-friendly content readingNewStringBuilder (old_text); Builder.append (text + ",");//Determines whether the search content already exists in the history file, and does not repeat the additionif (!old_text.contains (text + ",") {Sharedpreferences.editor Myeditor =Mysp.edit (); Myeditor.putstring ("History", builder.tostring ()); Myeditor.commit (); Toast.maketext (This, text + "Add Success"else {Toast.maketext ( this, text + "already exists" // clear search record public void Cleanhistory (View v) {sharedpreferences sp =getsharedpreferences ("Search_ History ", 0); Sharedpreferences.editor editor=sp.edit (); Editor.clear (); Editor.commit (); Toast.maketext (this, "clear success" super.ondestroy ();}      

Example Download >>>>

Android: Control Autocompletetextview Client Save search History auto Prompt

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.