十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
今天就跟大家聊聊有关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 ArrayAdaptermArrAdapter; private SharedPreferences mSharePreference; private LinearLayout mSearchHistoryLl; private List 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 (); 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 list = new ArrayList (); 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 (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 (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布局
看完上述内容,你们对Android应用中怎么实现一个搜索记录保存功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。