我如何使用 textwatcher 在 android studio 数据库中搜索
how can i search in android studio database with textwatcher
我想在 textwatcher 中使用搜索方法
我做任何事 bud cursor 总是 return false to me
任何人都可以帮助我!!!!
谢谢
这是我的数据库:
public static void searchSelect(CharSequence s) {
database = SQLiteDatabase.openOrCreateDatabase(destPath + "/database.db", null);
Cursor cursor = database.rawQuery("SELECT * FROM main WHERE title LIKE ' % " + s + " % ' ", null);
SearchFragment.search.clear();
Log.i("LOG", "cursor :" + cursor.moveToNext());
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
String address = cursor.getString(cursor.getColumnIndex("address"));
String imgAddress = cursor.getString(cursor.getColumnIndex("img_adrs"));
String city = cursor.getString(cursor.getColumnIndex("city"));
String category = cursor.getString(cursor.getColumnIndex("category"));
int cityId = cursor.getInt(cursor.getColumnIndex("city_id"));
int catId = cursor.getInt(cursor.getColumnIndex("cat_id"));
int inside = cursor.getInt(cursor.getColumnIndex("inside_id"));
Structure struct = new Structure(id, title, content, address, imgAddress, city, category, cityId, catId, inside);
struct.setId(id);
struct.setTitle(title);
struct.setContent(content);
struct.setAddress(address);
struct.setImgAddress(imgAddress);
struct.setCity(city);
struct.setCategory(category);
struct.setCity_id(cityId);
struct.setCat_id(catId);
struct.setInside(inside);
SearchFragment.search.add(struct);
}
cursor.close();
}
在我的片段中它是我的 textwatcher
private void TextWatcher() {
editText.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) {
Log.i("LOG", "text: " + s);
searchSelect(s.toString());
adapterSearch.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
您正在执行 2 cursor.moveToNext()
,结果未知。
实例化游标后,删除 Log 语句并执行:
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
// your code here
} while (cursor.moveToNext())
}
}
我想在 textwatcher 中使用搜索方法 我做任何事 bud cursor 总是 return false to me 任何人都可以帮助我!!!! 谢谢
这是我的数据库:
public static void searchSelect(CharSequence s) {
database = SQLiteDatabase.openOrCreateDatabase(destPath + "/database.db", null);
Cursor cursor = database.rawQuery("SELECT * FROM main WHERE title LIKE ' % " + s + " % ' ", null);
SearchFragment.search.clear();
Log.i("LOG", "cursor :" + cursor.moveToNext());
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
String address = cursor.getString(cursor.getColumnIndex("address"));
String imgAddress = cursor.getString(cursor.getColumnIndex("img_adrs"));
String city = cursor.getString(cursor.getColumnIndex("city"));
String category = cursor.getString(cursor.getColumnIndex("category"));
int cityId = cursor.getInt(cursor.getColumnIndex("city_id"));
int catId = cursor.getInt(cursor.getColumnIndex("cat_id"));
int inside = cursor.getInt(cursor.getColumnIndex("inside_id"));
Structure struct = new Structure(id, title, content, address, imgAddress, city, category, cityId, catId, inside);
struct.setId(id);
struct.setTitle(title);
struct.setContent(content);
struct.setAddress(address);
struct.setImgAddress(imgAddress);
struct.setCity(city);
struct.setCategory(category);
struct.setCity_id(cityId);
struct.setCat_id(catId);
struct.setInside(inside);
SearchFragment.search.add(struct);
}
cursor.close();
}
在我的片段中它是我的 textwatcher
private void TextWatcher() {
editText.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) {
Log.i("LOG", "text: " + s);
searchSelect(s.toString());
adapterSearch.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
您正在执行 2 cursor.moveToNext()
,结果未知。
实例化游标后,删除 Log 语句并执行:
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
// your code here
} while (cursor.moveToNext())
}
}