<?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