1. 程式人生 > >Android 從資料庫中快速搜尋匹配資料並新增監聽事件

Android 從資料庫中快速搜尋匹配資料並新增監聽事件

如何從資料庫中搜索與我們目標相符的資料呢?
我使用的是List view+cursoradapter。現在應該很少有人使用list view了吧,原來打算換換recyclerview來寫的,但是recyclerview不支援cursor view啊。暫且先記著,等日後研究出來了再改一改吧。

下面是效果圖:
這裡寫圖片描述

使用navicat進行模糊查詢,結果也是一樣的
這裡寫圖片描述
貼出核心程式碼:

package com.example.geekp.searchfromdatabase;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import
android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.view.View; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ImageView; import
android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.example.geekp.searchfromdatabase.dbDao.Config; import com.example.geekp.searchfromdatabase.dbDao.DBHelper; import com.example.geekp.searchfromdatabase.dbDao.DBUtil; import com.example.geekp.searchfromdatabase.dbDao.DBUtilConcreate; import
butterknife.BindView; import butterknife.ButterKnife; import static com.example.geekp.searchfromdatabase.GlobeContext.getContext; public class MainActivity extends AppCompatActivity { @BindView(R.id.edittext) EditText mEditText; @BindView(R.id.imageview) ImageView mImageView; @BindView(R.id.textview) TextView mTextView; @BindView(R.id.listview) ListView mListView; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); DBUtil dbUtil= DBUtilConcreate.getInstance(); dbUtil.initDataBase(); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); initView(); } @Override protected void onDestroy() { super.onDestroy(); cursor.close(); } private void initView() { //設定刪除圖片的點選事件 mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //把EditText內容設定為空 mEditText.setText(""); //把ListView隱藏 mListView.setVisibility(View.GONE); } }); //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) { //如果長度為0 if (s.length() == 0) { //隱藏“刪除”圖片 mImageView.setVisibility(View.GONE); } else {//長度不為0 //顯示“刪除圖片” mImageView.setVisibility(View.VISIBLE); //顯示ListView showListView(); } } //文字改變之後執行 public void afterTextChanged(Editable s) { } }); mTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //如果輸入框內容為空,提示請輸入搜尋內容 if (TextUtils.isEmpty(mEditText.getText().toString().trim())) { // ToastUtils.showToast(context,"請輸入您要搜尋的內容"); ToastUtils.showToast(getContext(), "請輸入您要搜尋的內容"); } else { //判斷cursor是否為空 if (cursor != null) { int columnCount = cursor.getCount(); if (columnCount == 0) { // ToastUtils.showToast(context, "對不起,沒有你要搜尋的內容"); ToastUtils.showToast(getContext(), "對不起,沒有你要搜尋的內容"); } } } } }); } private void showListView() { mListView.setVisibility(View.VISIBLE); //獲得輸入的內容 String str = mEditText.getText().toString().trim(); //獲取資料庫物件 // MyOpenHelper myOpenHelper = new MyOpenHelper(getContext()); DBHelper dbHelper = new DBHelper(getContext()); final SQLiteDatabase db = dbHelper.getReadableDatabase(); //得到cursor cursor = db.rawQuery("SELECT QuestionNum as _id,QuestionContent FROM Question WHERE QuestionContent LIKE '%" + str + "%'", null); SearchViewCursorAdapter adapter = new SearchViewCursorAdapter(getContext(), cursor); mListView.setAdapter(adapter); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //把cursor移動到指定行 cursor.moveToPosition(position); String name = cursor.getString(cursor.getColumnIndex(Config.COLUMN_QUESTIONCONTENT)); String itemname = cursor.getString(cursor.getColumnIndex("_id")); ToastUtils.showToast(getContext(), "點選了" + name); } }); } }
package com.example.geekp.searchfromdatabase;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.example.geekp.searchfromdatabase.dbDao.Config;


/**
 * Created by Administrator on 2016/5/21 0021.
 */
public class SearchViewCursorAdapter extends CursorAdapter {
    public SearchViewCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        //獲取view
        View view = View.inflate(context, R.layout.search_listview, null);
        //尋找控制元件
        viewHolder.textView = (TextView) view.findViewById(R.id.textview);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        ViewHolder viewHolder = (ViewHolder) view.getTag();
        //從cursor中獲取值
        String name =cursor.getString(cursor.getColumnIndex("_id"))+cursor.getString(cursor.getColumnIndex(Config.COLUMN_QUESTIONCONTENT));
        //把資料設定到控制元件上面
        viewHolder.textView.setText(name);

    }

    class ViewHolder {
        TextView textView;
    }
}

需要原始碼的請移步我的Github