Android 搜索框的实时查询/模糊查询

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/lijinweii/article/details/78175129

在搜索框的检索中我们经常会遇到“精确检索”、“模糊检索”,精确检索我就不多加解释了,我们看下模糊检索:

参考:
http://blog.csdn.net/jdsjlzx/article/details/46514761

http://www.cnblogs.com/jeffen/p/6958235.html

http://blog.csdn.net/ljd4305/article/details/39375163

这里实现的是搜索框时时查询,代码中是在数据库中使用的模糊查询;

整个布局使用的是线性布局,搜索框又是一个线性布局(里面包含一个相对布局和一个TextView,相对布局里面有一个EditText和ImageVIew),下面是一个ListView;

搜索框其实就是一个EditText,背景是用shape自己画出来的;上面放一个“删除”的图片;

在Activity中给EditText设置一个监听,当输入文字的时候获取输入的内容然后查询数据库,将查询到的数据展示到ListView中;

看下代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical">  
      
        <LinearLayout  
            android:layout_width="match_parent"  
            android:layout_height="43dp"  
            android:background="@color/colorPrimaryDark"  
            android:gravity="center"  
            android:orientation="horizontal">  
      
            <RelativeLayout  
                android:layout_width="0dp"  
                android:layout_height="match_parent"  
                android:layout_weight="1">  
      
                <EditText  
                    android:id="@+id/edittext"  
                    android:layout_width="match_parent"  
                    android:layout_height="match_parent"  
                    android:layout_marginBottom="8dp"  
                    android:layout_marginLeft="10dp"  
                    android:layout_marginTop="8dp"  
                    android:background="@drawable/searchbox_bj"  
                    android:hint="请输入搜索内容"  
                    android:maxLength="10"  
                    android:paddingLeft="15dp"  
                    android:singleLine="true"  
                    android:textSize="12sp" />  
      
                <ImageView  
                    android:id="@+id/imageview"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:layout_alignParentRight="true"  
                    android:layout_centerInParent="true"  
                    android:paddingRight="20dp"  
                    android:src="@drawable/delete"  
                    android:visibility="gone" />  
      
            </RelativeLayout>  
      
            <TextView  
                android:id="@+id/textview"  
                android:layout_width="wrap_content"  
                android:layout_height="match_parent"  
                android:gravity="center"  
                android:paddingLeft="10dp"  
                android:paddingRight="10dp"  
                android:text="搜索"  
                android:textColor="#ffffff"  
                android:textSize="20sp" />  
        </LinearLayout>  
      
        <ListView  
            android:id="@+id/listview"  
            android:layout_width="match_parent"  
            android:layout_height="0dp"  
            android:layout_weight="1"></ListView>  
    </LinearLayout>  

要在EditText文本改变前执行查询 数据库的操作并实时展示

    public class MainActivity extends Activity {  
      
        private EditText mEditText;  
        private ImageView mImageView;  
        private ListView mListView;  
        private TextView mTextView;  
        Context context;  
        Cursor cursor;  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            context = this;  
              
            initView();  
        }  
      
        private void initView() {  
            mTextView = (TextView) findViewById(R.id.textview);  
            mEditText = (EditText) findViewById(R.id.edittext);  
            mImageView = (ImageView) findViewById(R.id.imageview);  
            mListView = (ListView) findViewById(R.id.listview);  
      
            //设置删除图片的点击事件  
            mImageView.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    //把EditText内容设置为空  
                    mEditText.setText("");  
                    //把ListView隐藏  
                    mListView.setVisibility(View.GONE);  
                }  
            });  
      
            //EditText添加监听  
            mEditText.addTextChangedListener(new TextWatcher() {  
                  
                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() {  
                  
                public void onClick(View v) {  
                    //如果输入框内容为空,提示请输入搜索内容  
                    if(TextUtils.isEmpty(mEditText.getText().toString().trim())){  
                        ToastUtils.showToast(context,"请输入您要搜索的内容");  
                    }else {  
                        //判断cursor是否为空  
                        if (cursor != null) {  
                            int columnCount = cursor.getCount();  
                            if (columnCount == 0) {  
                                ToastUtils.showToast(context, "对不起,没有你要搜索的内容");  
                            }  
                        }  
                    }  
      
                }  
            });  
        }  
      
        private void showListView() {  
            mListView.setVisibility(View.VISIBLE);  
            //获得输入的内容  
            String str = mEditText.getText().toString().trim();  
            //获取数据库对象  
            MyOpenHelper myOpenHelper = new MyOpenHelper(getApplicationContext());  
            SQLiteDatabase db = myOpenHelper.getReadableDatabase();  
            //得到cursor  
            cursor = db.rawQuery("select * from lol where name like '%" + str + "%'", null);  
            MyListViewCursorAdapter adapter = new MyListViewCursorAdapter(context, 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("name"));  
                    ToastUtils.showToast(context, name);  
                }  
            });  
        }  
    }  
CharSequence s, int start, int before, int count(CharSequence s, int start, int before, int count)方法中做判断,s是EditText中的文本内容;
判断如果s长度为0隐藏“删除”图片,否则显示“删除图片”,显示ListView,查询数据库获得Cursor获得CursorAdapter将内容展示到ListView中;

源码:


猜你喜欢

转载自blog.csdn.net/lijinweii/article/details/78175129
今日推荐