使用SQLite ListView中的EditText搜索过滤器

时间:2015-09-15 13:55:53

标签: android sqlite listview

我的ListView正在从SQLite数据库中正确地进行提取,但我的问题是我添加了EditText来按名称进行过滤。因此,如果我在EditText中输入任何字母,它应该相应地过滤,但它不会发生。以下是我的代码:

MainActivity.java

public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
    ArrayList<StudentEntity> listentity = new ArrayList<StudentEntity>();
    String texto="";
    texto=inputSearch.getText().toString();

     String DB_PATH = "/data/data/com.naman.namrathasrinivas.choultriesbangalore" + "/databases/";
     String DB_NAME = "Marriage.sqlite";
    String mypath = DB_PATH + DB_NAME;
    db = SQLiteDatabase.openDatabase(mypath, null,
            SQLiteDatabase.OPEN_READWRITE);
    String selectQuery = "SELECT * FROM hall WHERE name LIKE '%\" + texto + \"%' ";
     Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            StudentEntity sentity = new StudentEntity();
            sentity.setName(cursor.getString(1));
            sentity.setLocality(cursor.getString(12));
            listentity.add(sentity);
        } while (cursor.moveToNext());
    }
}

1 个答案:

答案 0 :(得分:0)

您的SQL查询错误

"SELECT * FROM hall WHERE name LIKE '%\" + texto + \"%' ";

此处textto被视为字符串本身。

尝试以下查询:

"SELECT * FROM hall WHERE name LIKE '%" + texto + "%'";

这里&#34; textTo&#34;的值将在实际的SQL查询中连接而不是&#34; textTo&#34;本身。