本文实例为大家分享了AndroID搜索保存历史记录功能,供大家参考,具体内容如下
要点:就是缓存输入的内容到 本地 下面就是实现保存 搜索内容到本地 和 清空本地历史的方法
//保存搜索内容到本地 public voID save() { String text = mKeywordEt.getText().toString(); String oldText = mSharePreference.getString(SEARCH_HISTORY,""); StringBuilder builder = new StringBuilder(text); builder.append("," + oldText); if (!TextUtils.isEmpty(text) && !oldText.contains(text + ",")) { SharedPreferences.Editor myEditor = mSharePreference.edit(); myEditor.putString(SEARCH_HISTORY,builder.toString()); myEditor.commit(); } updateData(); } //清空本地历史 public voID cleanHistory() { SharedPreferences.Editor editor = mSharePreference.edit(); editor.clear(); editor.commit(); updateData(); mSearchHistoryLl.setVisibility(VIEw.GONE); Singletoast.show(this,getString(R.string.clear_history_success),Toast.LENGTH_SHORT); }
activity
import androID.content.SharedPreferences; import androID.os.Bundle; import androID.text.Editable; import androID.text.TextUtils; import androID.text.TextWatcher; import androID.vIEw.VIEw; import androID.Widget.AdapterVIEw; import androID.Widget.ArrayAdapter; import androID.Widget.EditText; import androID.Widget.ImageVIEw; import androID.Widget.linearLayout; import androID.Widget.ListVIEw; import androID.Widget.TextVIEw; import androID.Widget.Toast; import com.ccvIDeo.R; import com.yizhibo.vIDeo.adapter.SearchAdapter; import com.yizhibo.vIDeo.app.YZBApplication; import com.yizhibo.vIDeo.base.BaseListActivity; import com.yizhibo.vIDeo.utils.Constants; import com.yizhibo.vIDeo.utils.Singletoast; import com.yizhibo.vIDeo.utils.Utils; public class SearchListActivity extends BaseListActivity implements VIEw.OnClickListener { public static final String EXTRA_KEY_TYPE = "extra_key_type"; private static final String PRE_SEARCH_HISTORY = "pre_search_history"; private static final String SEARCH_HISTORY = "search_history"; private EditText mKeywordEt; private TextVIEw mOperationTv; private ArrayAdapter<String> mArrAdapter; private SharedPreferences mSharePreference; private linearLayout mSearchHistoryLl; private List<String> mHistoryKeywords; private ListVIEw mListVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSharePreference = YZBApplication.getApp().getSharedPreferences(PRE_SEARCH_HISTORY,0); setContentVIEw(R.layout.activity_search_List); mKeywordEt = (EditText) findVIEwByID(R.ID.tab_bar_keyword_et); mHistoryKeywords = new ArrayList<String>(); mKeywordEt.addTextChangedListener(new TextWatcher() { @OverrIDe public voID beforeTextChanged(CharSequence s,int start,int count,int after) { } @OverrIDe public voID onTextChanged(CharSequence s,int before,int count) { if (s.length() == 0) { mAdapter.clear(); mAdapter.notifyDataSetChanged(); mOperationTv.setText(R.string.cancel); mEmptyVIEw.hIDe(); clearKeywordiv.setVisibility(VIEw.GONE); if (mHistoryKeywords.size() > 0) { mSearchHistoryLl.setVisibility(VIEw.VISIBLE); } else { mSearchHistoryLl.setVisibility(VIEw.GONE); } } else { mSearchHistoryLl.setVisibility(VIEw.GONE); mOperationTv.setText(R.string.search); clearKeywordiv.setVisibility(VIEw.VISIBLE); } } @OverrIDe public voID afterTextChanged(Editable s) { } }); mKeywordEt.requestFocus(); mOperationTv = (TextVIEw) findVIEwByID(R.ID.tab_bar_cancel_tv); mOperationTv.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (mKeywordEt.getText().length() > 0) { hIDeinputMethod(); save(); } else { finish(); } } }); initSearchHistory(); } public voID initSearchHistory() { mSearchHistoryLl = (linearLayout) findVIEwByID(R.ID.search_history_ll); ListVIEw ListVIEw = (ListVIEw) findVIEwByID(R.ID.search_history_lv); findVIEwByID(R.ID.clear_history_btn).setonClickListener(this); String history = mPref.getString(Preferences.KEY_SEARCH_HISTORY_KEYWORD); if (!TextUtils.isEmpty(history)){ List<String> List = new ArrayList<String>(); for(Object o : history.split(",")) { List.add((String)o); } mHistoryKeywords = List; } if (mHistoryKeywords.size() > 0) { mSearchHistoryLl.setVisibility(VIEw.VISIBLE); } else { mSearchHistoryLl.setVisibility(VIEw.GONE); } mArrAdapter = new ArrayAdapter<String>(this,R.layout.item_search_history,mHistoryKeywords); ListVIEw.setAdapter(mArrAdapter); ListVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> adapterVIEw,VIEw vIEw,int i,long l) { mKeywordEt.setText(mHistoryKeywords.get(i)); mSearchHistoryLl.setVisibility(VIEw.GONE); } }); mArrAdapter.notifyDataSetChanged(); } public voID save() { String text = mKeywordEt.getText().toString(); String oldText = mPref.getString(Preferences.KEY_SEARCH_HISTORY_KEYWORD); if (!TextUtils.isEmpty(text) && !oldText.contains(text)) { mPref.putString(Preferences.KEY_SEARCH_HISTORY_KEYWORD,text + "," + oldText); mHistoryKeywords.add(0,text); } mArrAdapter.notifyDataSetChanged(); } public voID cleanHistory() { mPref.remove(Preferences.KEY_SEARCH_HISTORY_KEYWORD); mHistoryKeywords.clear(); mArrAdapter.notifyDataSetChanged(); mSearchHistoryLl.setVisibility(VIEw.GONE); Singletoast.show(this,Toast.LENGTH_SHORT); } public voID updateData(){ String history = mSharePreference.getString(SEARCH_HISTORY,""); mHistoryArr = history.split(","); mArrAdapter = new ArrayAdapter<String>(this,R.layout.activity_searchhistory,mHistoryArr); mListVIEw.setAdapter(mArrAdapter); mArrAdapter.notifyDataSetChanged(); } @OverrIDe public voID onClick(VIEw vIEw) { switch (vIEw.getID()) { case R.ID.clear_history_btn: cleanHistory(); break; } } }
下拉d出layout布局
<linearLayout androID:ID="@+ID/search_history_ll" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_below="@ID/global_search_action_bar_rl" androID:layout_height="wrap_content"> <TextVIEw androID:ID="@+ID/contentTextVIEw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="@dimen/text_size_Title_h2" androID:text="@string/search_history" androID:paddingleft="10dp" androID:textcolor="@color/text_gray"/> <ListVIEw androID:ID="@+ID/search_history_lv" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:cachecolorHint="@androID:color/transparent" androID:ListSelector="@drawable/List_item_selector"> </ListVIEw> <button androID:ID="@+ID/clear_history_btn" androID:layout_wIDth="210dp" androID:layout_height="@dimen/button_common_height" androID:layout_below="@ID/rise_crash_ll" androID:layout_margintop="5dp" androID:textcolor="@color/text_btn_selector" androID:layout_gravity="center" androID:textSize="@dimen/text_size_Title_h2" androID:layout_centerHorizontal="true" androID:text="@string/clear_search_history" androID:background="@drawable/round_btn_selector" /> </linearLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android实现搜索保存历史记录功能全部内容,希望文章能够帮你解决Android实现搜索保存历史记录功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)