利用SharedPreferences 保存已经输入过的历史记录
class文件
public class AutoComplite extends Activity implements OnClickListener { private AutoCompleteTextView mBaseAtComp; private AutoCompleteTextView mHistoryAComp; // 保存的xml的文件名 private static final String PREF = "MyHistory"; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.autocomplete); mBaseAtComp = (AutoCompleteTextView) findViewById(R.id.base_autoTextView); mHistoryAComp = (AutoCompleteTextView) findViewById(R.id.history_autoTextView); /** * 最基本的AutoCompleteTextView */ String[] countries = getResources().getStringArray(R.array.countries); ArrayAdapter<String> base = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, countries); mBaseAtComp.setAdapter(base); /** * 记录 */ initAutoComplete("history", mHistoryAComp); Button search = (Button) findViewById(R.id.search); search.setOnClickListener(this); } /** * 初始化AutoCompleteTextView,最多显示5项提示,使 AutoCompleteTextView在一开始获得焦点时自动提示 * * @param field 保存在sharedPreference中的字段名(key) * @param auto 要操作的AutoCompleteTextView */ private void initAutoComplete(String field, AutoCompleteTextView auto) { SharedPreferences sp = getSharedPreferences(PREF, 0); String longhistory = sp.getString(field, "nothing"); String[] hisArrays = longhistory.split(","); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, hisArrays); // 只保留最近的50条的记录 if (hisArrays.length > 50) { String[] newArrays = new String[50]; System.arraycopy(hisArrays, 0, newArrays, 0, 50); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, newArrays); } auto.setAdapter(adapter); auto.setDropDownHeight(350); auto.setThreshold(1); auto.setCompletionHint("最近的5条记录"); auto.setOnFocusChangeListener(new OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { AutoCompleteTextView view = (AutoCompleteTextView) v; if (hasFocus) { view.showDropDown(); } SharedPreferences sp = getSharedPreferences(PREF, 0); String longhistory = sp.getString("history", "nothing"); String[] hisArrays = longhistory.split(","); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, hisArrays); } }); } public void onClick(View v) { // TODO Auto-generated method stub saveHistory("history", mHistoryAComp); } /** * 保存数据时选择一个固定值做 “key“ * 这样再读取时才知道通过什么key来取值。 * @param field * @param auto */ private void saveHistory(String field, AutoCompleteTextView auto) { String addText = auto.getText().toString(); SharedPreferences sp = getSharedPreferences(PREF, 0); SharedPreferences.Editor edit = sp.edit(); String longhistory = sp.getString(field, "nothing"); if (!longhistory.contains(addText + ",")) { StringBuilder sb = new StringBuilder(longhistory); sb.insert(0, addText + ","); edit.putString("history", sb.toString()); edit.commit(); } } }
xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="简单的AutoComplete" /> <AutoCompleteTextView android:id="@+id/base_autoTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:completionThreshold="1" android:hint="Please Input" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="可记录用户输入历史的AutoComplete" /> <AutoCompleteTextView android:id="@+id/history_autoTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:completionThreshold="1" android:hint="Please Input" /> <Button android:id="@+id/search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Search" /> </LinearLayout>
保存的xml文件中的内容
这里全部存放于history的 key中,这样再读取值时才能全部读出。
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="history">AFeqq,AFeq,testavasdfe,testa,brush,fs,as,TEst,china,nothing</string>
</map>