ListView,SimpleCursorAdapter,一个EditTextfilter – 为什么它不会做任何事情?

我想过滤一个SimpleCursorAdapter驱动的ListView,在它上面有一个EditText框。 我有下面的代码,但是当我input框时,没有任何反应; 完整列表继续显示。 我究竟做错了什么?

mCursor = getDirectoryList(null); adapter = new SimpleCursorAdapter(this, R.layout.directory_people_item, mCursor, new String[]{ directoryPeople.LAST_NAME, directoryPeople.FIRST_NAME, directoryPeople.MIDDLE_NAME, directoryPeople.JOB_TITLE}, new int[]{ R.id.txtLastName, R.id.txtFirstName, R.id.txtMiddle, R.id.txtTitle} ); ListView av = (ListView)findViewById(R.id.listPeople); av.setAdapter(adapter); av.setFastScrollEnabled(true); av.setTextFilterEnabled(true); EditText etext=(EditText)findViewById(R.id.search_box); etext.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { ListView av = (ListView)findViewById(R.id.listPeople); SimpleCursorAdapter filterAdapter = (SimpleCursorAdapter)av.getAdapter(); filterAdapter.getFilter().filter(s.toString()); } }); adapter.setFilterQueryProvider(new FilterQueryProvider() { public Cursor runQuery(CharSequence constraint) { return getDirectoryList(constraint); } }); 

这里是getDirectoryList()

 public Cursor getDirectoryList (CharSequence constraint) { SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); queryBuilder.setTables( directoryPeople.PEOPLE_TABLE ); String asColumnsToReturn[] = { directoryPeople.PEOPLE_TABLE + "." + directoryPeople.LAST_NAME + "," + directoryPeople.PEOPLE_TABLE + "." + directoryPeople.FIRST_NAME + "," + directoryPeople.PEOPLE_TABLE + "." + directoryPeople.MIDDLE_NAME + "," + directoryPeople.PEOPLE_TABLE + "." + directoryPeople.JOB_TITLE + "," + directoryPeople.PEOPLE_TABLE + "." + directoryPeople._ID }; if (constraint == null || constraint.length () == 0) { // Return the full list return queryBuilder.query(mDB, asColumnsToReturn, null, null, null, null, directoryPeople.DEFAULT_SORT_ORDER); } else { return mDB.query(directoryPeople.PEOPLE_TABLE, asColumnsToReturn, "LAST_NAME like '%'" + constraint.toString() + "'%'", null, null, null, "CASE WHEN LAST_NAME like '" + constraint.toString() + "%' THEN 0 ELSE 1 END, LAST_NAME"); } } 

我已经尝试了我在search中遇到的每个示例和答案,但都无济于事。 这是令人沮丧的,你可以想像。 在此先感谢任何人可以帮助!

Solutions Collecting From Web of "ListView,SimpleCursorAdapter,一个EditTextfilter – 为什么它不会做任何事情?"

试试这个方法:

 } else { String value = "%"+constraint.toString()+"%"; return mDB.query(directoryPeople.PEOPLE_TABLE, asColumnsToReturn, "LAST_NAME like ? ", new String[]{value}, null, null, null); } 

我跳过了最后一个论点,因为我不知道该怎么办:

 "CASE WHEN LAST_NAME like '" + constraint.toString() + "%' THEN 0 ELSE 1 END, LAST_NAME" 

你有没有在模拟器的shell中试过这个查询?