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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義流式布局實(shí)現(xiàn)淘寶搜索記錄
- Android本地實(shí)現(xiàn)搜索歷史記錄
- Android流式布局實(shí)現(xiàn)歷史搜索記錄功能
- Android項(xiàng)目類(lèi)似淘寶 電商 搜索功能,監(jiān)聽(tīng)軟鍵盤(pán)搜索事件,延遲自動(dòng)搜索,以及時(shí)間排序的搜索歷史記錄的實(shí)現(xiàn)
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android實(shí)現(xiàn)簡(jiǎn)易計(jì)步器功能隔天步數(shù)清零查看歷史運(yùn)動(dòng)紀(jì)錄
- android中AutoCompleteTextView的簡(jiǎn)單用法(實(shí)現(xiàn)搜索歷史)
- Android中使用 AutoCompleteTextView 實(shí)現(xiàn)手機(jī)號(hào)格式化附帶清空歷史的操作
- Android實(shí)現(xiàn)搜索歷史功能
- Android實(shí)現(xiàn)歷史搜索記錄
相關(guān)文章
Android實(shí)現(xiàn)圖片轉(zhuǎn)高斯模糊以及高斯模糊布局
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片轉(zhuǎn)高斯模糊的方法,以及高斯模糊布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)可收縮和擴(kuò)展的TextView
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可收縮和擴(kuò)展的TextView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android本地存儲(chǔ)SharedPreferences詳解
這篇文章主要介紹了Android本地存儲(chǔ)SharedPreferences詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
多面分析HarmonyOS與Android的特點(diǎn)
請(qǐng)教身邊的大佬們,公司的CTO、中臺(tái)部門(mén)的總監(jiān)、老東家數(shù)十年行業(yè)經(jīng)驗(yàn)的老架構(gòu)、以及在中科院讀研究生的大學(xué)老室友、技術(shù)圈的網(wǎng)友等等,他們都給出了自己獨(dú)特的看法,讓我從多方面更好的去了解到了大家對(duì)鴻蒙的認(rèn)識(shí)2021-08-08
Android開(kāi)發(fā)之ViewSwitcher用法實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之ViewSwitcher用法,結(jié)合實(shí)例形式分析了ViewSwitcher的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02
Android TabLayout 實(shí)現(xiàn)底部Tab的示例代碼
本篇文章主要介紹了Android TabLayout 實(shí)現(xiàn)底部Tab的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
解決java.lang.NoClassDefFoundError: android.support.v4.animati
這篇文章主要介紹了解決Android Studio出現(xiàn)java.lang.NoClassDefFoundError: android.support.v4.animation.AnimatorCompatHelper的問(wèn)題,感興趣的朋友一起看看吧2021-08-08
Android之PreferenceActivity應(yīng)用詳解
為了引入這個(gè)概率 首先從需求說(shuō)起 即:現(xiàn)有某Activity專(zhuān)門(mén)用于手機(jī)屬性設(shè)置 那么應(yīng)該如何做呢2012-11-11

