如何在Android中进行搜索工作

时间:2013-03-22 03:20:23

标签: java android android-sqlite

我有以下代码,工作正常..我想添加一个搜索功能...你能告诉我我该怎么做以及在哪里

MyDatabase.java

package com.example.test4;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDatabase {
    public static final String MY_DB2 = "testdb5";
    MyHelper mh;
    Context myCon;
    SQLiteDatabase sdb;

    public MyDatabase(Context c) {
        myCon = c;
        mh = new MyHelper(myCon, MY_DB2, null, 1);
    }

    public void open() {
        sdb = mh.getWritableDatabase();
    }

    public class MyHelper extends SQLiteOpenHelper {

        public MyHelper(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table students(_id integer primary key, "
                    + "s_name text, " + "s_attendance text, "
                    + "s_homework text, " + "s_participation text, "
                    + "s_quiz integer, " + "s_test integer, "
                    + "s_comment text, " + "p_email text);");
            Log.d("1", "Table Created");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

    }

    public void StudentsInsert(ContentValues cv) {
        sdb.insert("students", null, cv);
    }

    public Cursor getStudents() {
        Cursor c = sdb.query("students", null, null, null, null, null, null);
        return c;
    }

    public Cursor filterStudents() {
        // SQLiteDatabase db = mh.getReadableDatabase();

        Cursor c = sdb.query("students", new String[] { "s_name",
                "s_attendance", "s_homework", "s_participation", "s_quiz",
                "s_test", "s_comment", "p_email" }, "s_name like %a%", null,
                null, null, null);
        return c;

    }

}

我认为我在filterStudents()中做错了什么......

这就是我在其他文件上的内容

EditText myFilter = (EditText) findViewById(R.id.myFilter);
        myFilter.addTextChangedListener(new TextWatcher() {

         public void afterTextChanged(Editable s) {
         }

         public void beforeTextChanged(CharSequence s, int start, 
           int count, int after) {
         }

         public void onTextChanged(CharSequence s, int start, 
           int before, int count) {
          sca.getFilter().filter(s.toString());
         }
        });

        sca.setFilterQueryProvider(new FilterQueryProvider() {
               public Cursor runQuery(CharSequence constraint) {
                   return mdb.filterStudents();
               }
           });

3 个答案:

答案 0 :(得分:0)

尝试在过滤字符串周围加上引号。即,而不是:

like %a%

成功:

like '%a%'

保持双引号(“)的方式,但在过滤字符串周围添加单引号。

答案 1 :(得分:0)

我使用这种方式可能对你有所帮助。

创建适配器以将数据设置为listview。

创建列表视图以显示数据。

myFilter.addTextChangedListener(new TextWatcher() {         
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count){                                
      myadapter.getFilter().filter(s);
      listview.setAdapter(myadapter);    
      myadapter.notifyDataSetChanged(); 
}

MyDatabase mdb = new MyDatabase(this);
//set adapter as per requirement this is sample.
myadapter= new MySimpleCursorAdapter(this, R.layout.list_search, m_curContacts, fields, new int []{R.id.textView1,R.id.imageView1});
    myadapter.setFilterQueryProvider(new FilterQueryProvider() {            
    public Cursor runQuery(CharSequence constraint) {
        //check here cursor is getting any result or not.if not then problem with your query.   
            return mdb.filterStudents();
    }

});

您必须在数据库中以这种方式使用"'%"+ value+ "%'"

答案 2 :(得分:0)

我找到了解决问题的方法..我想分享一下..

我的代码看起来像这样

EditText myFilter = (EditText) findViewById(R.id.myFilter);
        myFilter.addTextChangedListener(new TextWatcher() {

         public void afterTextChanged(Editable s) {
         }

         public void beforeTextChanged(CharSequence s, int start, 
           int count, int after) {
         }

         public void onTextChanged(CharSequence s, int start, 
           int before, int count) {
          sca.getFilter().filter(s.toString());
         }
        });

        sca.setFilterQueryProvider(new FilterQueryProvider() {
               public Cursor runQuery(CharSequence constraint) {
                   return mdb.fetchMyValues(constraint.toString());
               }
           });

并在我的数据库文件中添加了以下代码

public Cursor fetchMyValues(String inputText) {

        String DATABASE_TABLE="students";
        return sdb.query(DATABASE_TABLE, new String[] {"_id", "s_name",
                "s_attendance", "s_homework", "s_participation", "s_quiz",
                "s_test", "s_comment", "p_email"}, 
                  "s_name like '%" + inputText +"%'"  , null, null, null, null);}

现在我的搜索效果很好......

谢谢