我有以下代码可以正常工作..我想添加一个搜索功能...你能告诉我我应该做什么以及在哪里
我的数据库.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();
}
});