很多时候我们在进行构造搜索栏查找的时候需要做一个模糊查找的效果。本文将教大家如何在客户端中构造出模糊查询的效果。原理是构建数据库,然后往数据库添加数据,接着对搜索进行监听,获取搜索的内容,通过数据库的模糊查找去获取对应的模糊查找的数据。
MainActivity.java
public class MainActivity extends Activity {
private EditText mEditText;//搜索栏
private ListView mListView;//显示模糊查找返回的数据
private Context mContext;
private String[] mItems = {"五花肉", "天涯海角", "小葱拌豆腐"};
private String[] mNumbers = {"1234556676", "2342352345", "3243545666"};
private List<SearchData> mData;//模糊查找的时候返回的数据
private SearchAdapter mSearchAdapter;//ListView的适配
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
initView();
}
//初始化View
private void initView() {
mEditText = (EditText) findViewById(R.id.et_search);
mListView = (ListView) findViewById(R.id.lv);
//给EditText添加搜索栏控件文本变化监听器
mEditText.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) {
}
@Override
public void afterTextChanged(Editable s) {
search();//文本输入发生变化时开始进行模糊查找
}
});
createSearch();
}
//创建数据库,并给数据库添加数据
private void createSearch() {
//打开或创建search.db数据库
SQLiteDatabase db = openOrCreateDatabase("search.db", Context.MODE_PRIVATE, null);
//在数据库中创建mydata表,如果该表存在,则先删除后再创建,该表有三个字段,一个是id,一个是姓名(name),一个是编号(number)
db.execSQL("DROP TABLE IF EXISTS mydata");
db.execSQL("CREATE TABLE mydata (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, number VARCHAR)");
//给数据库插入数据
int len = mItems.length;
for (int i = 0; i < len; i++) {
ContentValues cv = new ContentValues();
cv.put("name", mItems[i]);
cv.put("number", mNumbers[i]);
db.insert("mydata", null, cv);
}
}
//搜索栏文本变化时开始进行模糊查找
private void search() {
String temp = mEditText.getText().toString().trim();
mData = new ArrayList<>();
if (!temp.equals("")) {
SQLiteDatabase db_search = openOrCreateDatabase("search.db", Context.MODE_PRIVATE, null);
Cursor csearch = db_search.rawQuery("SELECT * FROM mydata WHERE name LIKE '%" + temp + "%'", null);
while (csearch.moveToNext()) {
String name = csearch.getString(csearch.getColumnIndex("name"));
String number = csearch.getString(csearch.getColumnIndex("number"));
SearchData data = new SearchData();
data.setName(name);
data.setNumber(number);
mData.add(data);
}
db_search.close();
csearch.close();
}
if (mSearchAdapter == null) {
mSearchAdapter = new SearchAdapter(mContext, mData);
mListView.setAdapter(mSearchAdapter);
} else {
mSearchAdapter.refreshData(mData);
}
}
}
SearchAdapter.java
public class SearchAdapter extends MyBaseAdapter<SearchData> {
public SearchAdapter(Context context, List<SearchData> data) {
super(context, data);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HolderView holderView = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
holderView = new HolderView();
holderView.name = (TextView) convertView.findViewById(R.id.name);
holderView.number = (TextView) convertView.findViewById(R.id.number);
convertView.setTag(holderView);
} else {
holderView = (HolderView) convertView.getTag();
}
holderView.name.setText(mData.get(position).getName());
holderView.number.setText(mData.get(position).getNumber());
return convertView;
}
private class HolderView {
TextView name;
TextView number;
}
}
MyBaseAdapter.java
public abstract class MyBaseAdapter<T> extends BaseAdapter {
protected List<T> mData;
protected Context mContext;
public MyBaseAdapter(Context context, List<T> data) {
this.mContext = context;
this.mData = data;
}
public void refreshData(List<T> data) {
if (mData != null) {
mData.clear();
mData.addAll(data);
}
this.notifyDataSetChanged();
}
public void refreshDataNull(List<T> data) {
if (mData != null) {
mData.clear();
mData.addAll(data);
}else {
mData.clear();
mData.addAll(data);
}
this.notifyDataSetChanged();
}
public void addData(List<T> data) {
if (mData != null) {
mData.addAll(data);
}
this.notifyDataSetChanged();
}
public void clearAllData() {
if (mData != null) {
mData.clear();
}
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return mData == null ? 0 : mData.size();
}
@Override
public Object getItem(int position) {
return mData == null ? 0 : mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public List<T> getData() {
return mData;
}
public void setData(List<T> data) {
this.mData = data;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:id="@+id/tv_search"
android:text="搜索"
android:textColor="#000000"
android:textSize="25sp"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/et_search"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_weight="1"
android:id="@+id/lv"
android:layout_height="0dp">
</ListView>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:background="#ffffff"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:id="@+id/name"
android:gravity="center"
android:textSize="16sp"
android:textColor="#000000"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="16sp"
android:textColor="#000000"
android:id="@+id/number"
android:layout_height="match_parent" />
</LinearLayout>