全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# Android搜索保存历史记录  # Android保存历史记录  # Android搜索  # Android自定义流式布局实现淘宝搜索记录  # Android本地实现搜索历史记录  # Android流式布局实现历史搜索记录功能  # Android项目类似淘宝 电商 搜索功能  # 监听软键盘搜索事件  # 延迟自动搜索  # 以及时间排序的搜索历史记录的实现  # Android实现搜索功能并本地保存搜索历史记录  # Android实现简易计步器功能隔天步数清零查看历史运动纪录  # android中AutoCompleteTextView的简单用法(实现搜索历史)  # Android中使用 AutoCompleteTextView 实现手机号格式化附带清空历史的操作  # Android实现搜索历史功能  # Android实现历史搜索记录  # 清空  # 弹出  # 历史记录  # 大家分享  # 具体内容  # 大家多多  # ListView  # TextView  # ccvideo  # EditText  # ImageView  # LinearLayout  # SearchAdapter  # app  # YZBApplication  # yizhibo  # video  # adapter  # content  # os 


相关文章: 网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?  建站主机选择指南:服务器配置与SEO优化实战技巧  如何选购建站域名与空间?自助平台全解析  如何通过虚拟主机快速完成网站搭建?  如何续费美橙建站之星域名及服务?  西安专业网站制作公司有哪些,陕西省建行官方网站?  Android自定义listview布局实现上拉加载下拉刷新功能  建站之星收费标准详解:套餐费用及年费价格表一览  如何快速完成中国万网建站详细流程?  宝塔建站教程:一键部署配置流程与SEO优化实战指南  网站海报制作教学视频教程,有什么免费的高清可商用图片网站,用于海报设计?  建站主机服务器选购指南:轻量应用与VPS配置解析  nginx修改上传文件大小限制的方法  网站制作知乎推荐,想做自己的网站用什么工具比较好?  开心动漫网站制作软件下载,十分开心动画为何停播?  如何快速查询网址的建站时间与历史轨迹?  如何在腾讯云服务器快速搭建个人网站?  江苏网站制作公司有哪些,江苏书法考级官方网站?  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?  如何通过山东自助建站平台快速注册域名?  如何选择适配移动端的WAP自助建站平台?  七夕网站制作视频,七夕大促活动怎么报名?  网站制作公司广州有几家,广州尚艺美发学校网站是多少?  网站app免费制作软件,能免费看各大网站视频的手机app?  长春网站建设制作公司,长春的网络公司怎么样主要是能做网站的?  建站之星安装路径如何正确选择及配置?  ,怎么用自己头像做动态表情包?  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  如何在IIS中新建站点并解决端口绑定冲突?  合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?  平台云上自主建站:模板化设计与智能工具打造高效网站  ,怎么在广州志愿者网站注册?  如何优化Golang Web性能_Golang HTTP服务器性能提升方法  免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?  常州自助建站工具推荐:低成本搭建与模板选择技巧  网站制作网站,深圳做网站哪家比较好?  微信推文制作网站有哪些,怎么做微信推文,急?  如何确认建站备案号应放置的具体位置?  高防服务器租用指南:配置选择与快速部署攻略  头像制作网站在线制作软件,dw网页背景图像怎么设置?  在线制作视频网站免费,都有哪些好的动漫网站?  建站之星如何保障用户数据免受黑客入侵?  早安海报制作网站推荐大全,企业早安海报怎么每天更换?  专业网站制作服务公司,有哪些网站可以免费发布招聘信息?  建站之星Pro快速搭建教程:模板选择与功能配置指南  微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?  浅谈Javascript中的Label语句  网站制作中优化长尾关键字挖掘的技巧,建一个视频网站需要多少钱?  如何在Golang中使用replace替换模块_指定本地或远程路径 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。