1

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

我的数据库.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();
               }
           });
4

3 回答 3

0

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

like %a%

做了:

like '%a%'

保持双引号 (") 保持原样,但在过滤器字符串周围添加单引号。

于 2013-03-22T03:27:08.230 回答
0

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

创建用于将数据设置到列表视图的适配器。

创建用于显示数据的列表视图。

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+ "%'"在数据库中使用这种方式。

于 2013-03-22T04:52:41.457 回答
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);}

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

谢谢

于 2013-03-22T15:38:41.147 回答