搜索歷史存本地

內容:像淘寶搜索欄一樣,將搜索過得內容保存並顯示在搜索歷史

代碼如下:

佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.leixiansheng.localhistorysearch.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_margin="10dp">

        <EditText
            android:id="@+id/et_search"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:background="@drawable/search_background"
            android:padding="5dp"/>

        <TextView
            android:id="@+id/tv_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="搜索"
            android:textSize="18sp" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="搜索歷史"
        android:textSize="18sp" />

    <View
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_margin="10dp"/>

    <LinearLayout
        android:id="@+id/ll_search"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp">
    </LinearLayout>
</LinearLayout>

LocalSearchUtil

package com.example.leixiansheng.localhistorysearch;

import android.content.Context;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Created by Leixiansheng on 2018/6/26.
 */

public class LocalSearchUtil {
	private static final String LOCAL_SEARCH = "local_search";

	public static List<String> getLocalSearch(Context context){
		String longHistory = (String) SPUtils.getInstance(context).getString(LOCAL_SEARCH, "");
		String[] tmpHistory = longHistory.split(",");                            //split後長度爲1有一個空串對象
		List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory));

		if (historyList.size() == 1 && historyList.get(0).equals("")) {          //如果沒有搜索記錄,split之後第0位是個空串的情況下
			historyList.clear();                                                 //清空集合,這個很關鍵
		}

		return historyList;
	}

	public static void saveLocalSearch(Context context, String inputText) {
		SPUtils spUtils = SPUtils.getInstance(context);
		if (TextUtils.isEmpty(inputText)) {
			return;
		}
		String longHistory = spUtils.getString(LOCAL_SEARCH, "");        //獲取之前保存的歷史記錄
		String[] tmpHistory = longHistory.split(",");                            //逗號截取 保存在數組中
		List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory));          //將改數組轉換成ArrayList
		if (historyList.size() > 0) {
			//移除之前重複添加的元素
			for (int i = 0; i < historyList.size(); i++) {
				if (inputText.equals(historyList.get(i))) {
					historyList.remove(i);
					break;
				}
			}

			historyList.add(0, inputText);                           //將新輸入的文字添加集合的第0位也就是最前面

			if (historyList.size() > 10) {
				historyList.remove(historyList.size() - 1);         //最多保存10條搜索記錄 刪除最早搜索的那一項
			}

			//逗號拼接
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < historyList.size(); i++) {
				sb.append(historyList.get(i) + ",");
			}
			//保存到sp
			spUtils.putString(LOCAL_SEARCH, sb.toString());
		} else {
			//之前未添加過
			spUtils.putString(LOCAL_SEARCH, inputText + ",");
		}
	}
}

Main

package com.example.leixiansheng.localhistorysearch;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

	@BindView(R.id.et_search)
	EditText mEtSearch;
	@BindView(R.id.ll_search)
	LinearLayout mLlSearch;
	@BindView(R.id.tv_search)
	TextView mTvSearch;

	private List<String> mSearchList;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ButterKnife.bind(this);

		loadSearch();
		mTvSearch.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String s = mEtSearch.getText().toString();
				if (!TextUtils.isEmpty(s)) {
					LocalSearchUtil.saveLocalSearch(MainActivity.this, s);
				}
				loadSearch();
			}
		});
	}

	private void loadSearch() {
		mLlSearch.removeAllViews();
		mSearchList = LocalSearchUtil.getLocalSearch(this);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		for (int i = 0; i < mSearchList.size(); i++) {
			final TextView textView = new TextView(this);
			textView.setTextSize(20);
			textView.setText(mSearchList.get(i));
			textView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(MainActivity.this, "click:"+textView.getText(),Toast.LENGTH_SHORT).show();
				}
			});
			mLlSearch.addView(textView, params);
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章