Android實(shí)現(xiàn)搜索保存歷史記錄功能
本文實(shí)例為大家分享了Android搜索保存歷史記錄功能,供大家參考,具體內(nèi)容如下
要點(diǎn):就是緩存輸入的內(nèi)容到 本地 下面就是實(shí)現(xiàn)保存 搜索內(nèi)容到本地 和 清空本地歷史的方法
//保存搜索內(nèi)容到本地 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 start, 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, getString(R.string.clear_history_success), 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; } } }
下拉彈出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" style="?android:buttonBarButtonStyle"/> </LinearLayout>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義流式布局實(shí)現(xiàn)淘寶搜索記錄
- Android本地實(shí)現(xiàn)搜索歷史記錄
- Android流式布局實(shí)現(xiàn)歷史搜索記錄功能
- Android項(xiàng)目類似淘寶 電商 搜索功能,監(jiān)聽軟鍵盤搜索事件,延遲自動(dòng)搜索,以及時(shí)間排序的搜索歷史記錄的實(shí)現(xiàn)
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android實(shí)現(xiàn)簡易計(jì)步器功能隔天步數(shù)清零查看歷史運(yùn)動(dòng)紀(jì)錄
- android中AutoCompleteTextView的簡單用法(實(shí)現(xiàn)搜索歷史)
- Android中使用 AutoCompleteTextView 實(shí)現(xiàn)手機(jī)號(hào)格式化附帶清空歷史的操作
- Android實(shí)現(xiàn)搜索歷史功能
- Android實(shí)現(xiàn)歷史搜索記錄
相關(guān)文章
Android 8.0 中如何實(shí)現(xiàn)視頻通話的畫中畫模式的示例
本篇文章介紹了Android 8.0 中如何實(shí)現(xiàn)視頻通話的畫中畫模式的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android BLE 藍(lán)牙開發(fā)之實(shí)現(xiàn)掃碼槍基于BLESSED開發(fā)
這篇文章主要介紹了Android BLE 藍(lán)牙開發(fā)之實(shí)現(xiàn)掃碼槍基于BLESSED開發(fā),示例代碼介紹了第三方庫BLESSED for Android的使用,需要的朋友可以參考下2022-03-03Android實(shí)現(xiàn)Ant Design 自定義表單組件
Ant Design 組件提供了Input,InputNumber,Radio,Select,uplod等表單組件,下面通過本文給大家詳細(xì)介紹Android實(shí)現(xiàn)Ant Design 自定義表單組件,需要的的朋友參考下吧2017-06-06解決Android WebView攔截url,視頻播放加載失敗的問題
這篇文章主要介紹了解決Android WebView攔截url,視頻播放加載失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android?Studio實(shí)現(xiàn)簡單繪圖板
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡單繪圖板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android convinientbanner頂部廣告輪播控件使用詳解
這篇文章主要為大家詳細(xì)介紹了Android convinientbanner頂部廣告輪播控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android6.0動(dòng)態(tài)申請(qǐng)權(quán)限所遇到的問題小結(jié)
這篇文章給大家介紹了Android6.0動(dòng)態(tài)申請(qǐng)權(quán)限所遇到的問題,在沒給大家介紹這下問題之前,先給大家說下基本定義和基本使用方式,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,對(duì)android 6.0 動(dòng)態(tài)權(quán)限遇到問題感興趣的朋友一起看看吧2016-11-11Flutter實(shí)現(xiàn)自定義篩選框的示例代碼
本文主要介紹了Flutter實(shí)現(xiàn)自定義篩選框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07