我知道这已经被介绍过很多次了,但我无法让它发挥作用。搜索过滤功能似乎不适用于我的ListView
. 谁能帮我理解我的代码有什么问题?结果从我的数据库中提取得很好,但即使在我在我的EditText
字段中输入文本后也没有过滤。我尝试EditText
使用 Watcher 测试 on 关键更改,它工作正常,除了我的列表过滤。
非常感激。
public class Interface extends Activity implements OnClickListener,
AdapterView.OnItemClickListener {
MyDbHelper mHelper;
SQLiteDatabase mDb;
Cursor mCursor;
SimpleCursorAdapter mAdapter, madapter;
ListView mList;
EditText returnsearch;
int textlength = 0;
public static final String TABLE_NAME = "hohom";
public static final String COL_SanID = "SandIdc";
public static final String COL_SanTitle = "SandTitleC";
public static final String COL_SanCat = "SandCatC";
public static final String COL_SanReadyin = "SandReadyinC";
public static final String COL_SandServing = "SandServingC";
public static final String COL_SandIngred = "SandIngredC";
public static final String COL_SandDirect = "SandDirectC";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipesearch);
mHelper = new MyDbHelper(this);
mList = (ListView) findViewById(R.id.listlist);
mList.setOnItemClickListener(this);
mList.setTextFilterEnabled(true);
returnsearch = (EditText) findViewById(R.id.searchrecipe);
Bundle extrass = getIntent().getExtras();
mDb = mHelper.getWritableDatabase();
String Type = extrass.getString("CategoryType");
String[] columns = new String[] {
"_id", COL_SanID, COL_SanTitle, COL_SanCat, COL_SanReadyin,
COL_SandServing, COL_SandIngred, COL_SandDirect };
mCursor = mDb.query(TABLE_NAME, columns, COL_SanCat + "=" + "?",
new String[] { Type }, null, null, null);
final String[] headers = new String[] { MyDbHelper.COL_SanTitle,
MyDbHelper.COL_SanReadyin, MyDbHelper.COL_SandServing };
mAdapter = new SimpleCursorAdapter(this, R.layout.listtype, mCursor,
headers, new int[] { R.id.listmaintitle, R.id.listreadyin,
R.id.listserving });
mList.setAdapter(mAdapter);
}
@Override
protected void onResume() {
super.onResume();
returnsearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
mAdapter.getFilter().filter(s);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
mAdapter.getFilter().filter(s);
mAdapter.notifyDataSetChanged();
}
});
}
public void onClick(View v)
{
}
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
mCursor.moveToPosition(position);
Intent b = new Intent(Interface.this, Recipe.class);
String rowId = mCursor.getString(0);
String Title = mCursor.getString(2);
String Category = mCursor.getString(3);
String Readyin = mCursor.getString(4);
String Serving = mCursor.getString(5);
String Ingredients = mCursor.getString(6);
String Directions = mCursor.getString(7);
b.putExtra("Title", Title);
b.putExtra("Category", Category);
b.putExtra("Readyin", Readyin);
b.putExtra("Serving", Serving);
b.putExtra("Ingredients", Ingredients);
b.putExtra("Directions", Directions);
startActivity(b);
}
}