因为要将网页搜索产生的历史记录保存到SharedPreferences中,当然这不是一个好办法,正常的应该是保存到数据库中, 通过字符串和文件多次读取会影响速度的,并且也很麻烦,如果确实要保存到SharedPreferences的话可供参考.

上文: 关于SharedPreferences读取键值的文章- http://blog.foreverlove.us/1658.html

程序的最终截图:

history

程序源码:

1. 定义一些常量

private final String PREF_HISTORY_LIST_STRING = “pre_historyListString”;
private final String SPLIT1 = “﹋”;    //分隔符一
private final String SPLIT2 = “﹌”;    //分隔符二
private final int MAXHISTORY = 30; //最多保存的历史记录条数

private List<HistoryList> mHistoryList = new ArrayList<HistoryList>();

2.historyList类

public class HistoryList {

    public int getIcon() {
        return icon;
    }

    /**
     * @param icon
     *            the icon to set
     */
    public void setIcon(int icon) {
        this.icon = icon;
    }

    /**
     * @return the keyword
     */
    public String getKeyword() {
        return keyword;
    }

    /**
     * @param keyword
     *            the keyword to set
     */
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    /**
     * @return the searchenginetext
     */
    public String getSearchenginetext() {
        return searchenginetext;
    }

    /**
     * @param searchenginetext
     *            the searchenginetext to set
     */
    public void setSearchenginetext(String searchenginetext) {
        this.searchenginetext = searchenginetext;
    }

    // TODO
    private int icon;
    private String keyword;
    private String searchenginetext;
}

3.检查是否有相同的历史记录,如果有的话删除以前存在的那条,在历史记录最后面追加新的记录,并同时检测历史记录的条数是否超过预设值,如果超过,则删除第一条CheckCommonItem();

private void CheckCommonItem(String Keywords, String SearchEngineName) {// need
        String words = Keywords + SPLIT1 + SearchEngineName;
        SharedPreferences settings = getActivity().getPreferences(
                Activity.MODE_PRIVATE);
        String historyString = settings.getString(PREF_HISTORY_LIST_STRING, “”);// 获得用户字串
        if (historyString == “”)
            return;
        String[] lines = historyString.split(SPLIT2);
        if (lines.length >= MAXHISTORY) {
            historyString = historyString.substring(historyString
                    .indexOf(SPLIT2) + 1);
            lines = historyString.split(SPLIT2);
        }
        for (int i = 0; i < lines.length; i++) {
            if (lines[i].equals(words)) {
                // 删除这个
                int wordsStart = historyString.indexOf(words);
                int wordsEnd = historyString.indexOf(words) + words.length();
                Log.i(TAG, “word,开始位置=” + wordsStart + “,结束位置=” + wordsEnd);

                if (i == 0) {
                    if (lines.length == 1) {
                        historyString = “”;
                    } else {
                        historyString = historyString.substring(
                                words.length() + 1, historyString.length());
                    }

                } else {

                    if (i < lines.length – 1) {
                        historyString = historyString.substring(0,
                                historyString.indexOf(words + SPLIT2) – 1)
                                +

                                historyString.substring(historyString
                                        .indexOf(words + SPLIT2)
                                        + words.length(), historyString
                                        .length());
                    } else if (i == lines.length – 1) {
                        historyString = historyString.substring(0,
                                historyString.lastIndexOf(words) – 1);

                    }
                }

            }
        }

        Editor editor = settings.edit();// 编辑器记录
        editor.putString(PREF_HISTORY_LIST_STRING, historyString);
        editor.commit();// 编辑器提交保存

    }

4.保存历史记录SaveSearchHistoryItemInfo

// 保存
    private void SaveSearchHistoryItemInfo(String Keywords,
            String SearchEngineName) {

        CheckCommonItem(Keywords, SearchEngineName);

        SharedPreferences settings = getActivity().getPreferences(
                Activity.MODE_PRIVATE);
        String historyString = settings.getString(PREF_HISTORY_LIST_STRING, “”);// 获得用户字串

        if (historyString == “” || historyString == null) {
            historyString = Keywords + SPLIT1 + SearchEngineName;
        } else {
            historyString = historyString + SPLIT2 + Keywords + SPLIT1
                    + SearchEngineName;
        }

        Log.i(TAG, “historyString=” + historyString);
        Editor editor = settings.edit();// 编辑器记录
        //       
        editor.putString(PREF_HISTORY_LIST_STRING, historyString);
        editor.commit();// 编辑器提交保存
        //notifyDataSetChanged();
    }

5.第一次加载程序的时候初始化历史记录InitHistoryList

// 读取
private void InitHistoryList() {
    mHistoryList = new ArrayList<HistoryList>();

    SharedPreferences settings = XNoteGlobal.getActivity().getPreferences(
            Activity.MODE_PRIVATE);
    String historyString = settings.getString(PREF_HISTORY_LIST_STRING, “”);// 获得用户字串
    Log.i(TAG, “historyString=” + historyString);
    if (historyString.equals(“”) || historyString == “”
            || historyString == null)
        return;
    String[] lines = historyString.split(SPLIT2);
    if (lines == null)
        return;
    for (int i = lines.length – 1; i >= 0; i–) {
        // 分隔

        String[] items = lines[i].split(SPLIT1);
        if (items == null)
            return;
        HistoryList mHistory = new HistoryList();

        // HashMap<String, Object> list1 = new HashMap<String, Object>();

        if (items[0] != null) {
            mHistory.setKeyword(items[0]);
        }
        // list1.put(“keyword”, items[0]);
        if (items.length > 1) {
            // list1.put(“searchEngineText”, items[1]);
            mHistory.setSearchenginetext(items[1]);
            mHistory.setIcon(getSearchIconFromSearchName(items[1]));
        } else {
            // list1.put(“searchEngineText”, “”);
            mHistory.setSearchenginetext(“”);
            mHistory.setIcon(0);
        }

        mHistoryList.add(mHistory);
        // mHistoryList.add(list1);
        mHistory = null;
    }
}

6.清除历史记录removeAllHistyory

// 清除
    private void removeAllHistyory() {
        SharedPreferences settings = XNoteGlobal.getActivity().getPreferences(
                Activity.MODE_PRIVATE);
        settings.edit().remove(PREF_HISTORY_LIST_STRING).commit();

        //SendMessage(3);

        //notifyDataSetChanged();
    }

7.历史记录ListView的Adapter

public class WebHistoryListAdapter extends
        ArrayAdapter<HistoryList> {
    Context mContext;
    LayoutInflater infalter;
    private LinearLayout view = null;

    public WebHistoryListAdapter (Context context,
            List<HistoryList> weblist) {
        super(context, 0, weblist);
        mContext = context;
        infalter = LayoutInflater.from(mContext);
        System.gc();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final HistoryList model = getItem(position);
        if (convertView == null || convertView.getTag() != model) {
            final LinearLayout view1 = (LinearLayout) infalter.inflate(
                    R.layout.insert_web_history_item, null);
            view = view1;
            ImageView mIcon = (ImageView) view1.findViewById(R.id.item_icon);
            if (model.getIcon() == 0) {
            } else {
                mIcon.setImageResource(model.getIcon());
            }
            TextView mKeyWord = (TextView) view1
                    .findViewById(R.id.item_keyword);
            mKeyWord.setText(model.getKeyword());
            TextView mSearchText = (TextView) view1
                    .findViewById(R.id.item_searchenginetext);
            mSearchText.setText(model.getSearchenginetext());

        } else {
            view = (LinearLayout) convertView;
        }
        view.setTag(model);
        return view;
    }

}

8.绑定数据

WebHistoryListAdapter mWebHistoryListAdapter = new WebHistoryListAdapter(
                getContext(), mHistoryList);
mHistoryListView.setAdapter(mWebHistoryListAdapter);