标签:os ar java for sp 文件 问题 ad ef
============问题描述============
public void save() { // 获取搜索框信息 String text = auto.getText().toString(); boolean add=true; SharedPreferences mysp = getSharedPreferences("search_history", 0); String old_text = mysp.getString("history", ""); StringBuilder builder = new StringBuilder(old_text); // 利用StringBuilder的append方法新增内容,逗号便于读取内容时用逗号拆分开 builder.append(text + ","); //如何判断记录已经存在? String[] history_arr = old_text.split(","); for(int i = 0;i<history_arr.length;i++){ if(history_arr[i]==text){ add=false; break; } } if(add){ SharedPreferences.Editor myeditor = mysp.edit(); myeditor.putString("history", builder.toString()); myeditor.commit(); Toast.makeText(this, text+"添加成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, text+"已存在", Toast.LENGTH_SHORT).show(); } }
============解决方案1============
============解决方案2============
怎么判断SharedPreferences 的某个记录已存在呢
标签:os ar java for sp 文件 问题 ad ef
原文地址:http://www.cnblogs.com/liangxieliang56/p/4038753.html