Android实现搜索保存历史记录功能
程序员文章站
2023-12-04 17:19:52
本文实例为大家分享了android搜索保存历史记录功能,供大家参考,具体内容如下
要点:就是缓存输入的内容到 本地 下面就是实现保存 搜索内容到本地 和 清空本地历史的方...
本文实例为大家分享了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 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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。