0

我知道这已经被介绍过很多次了,但我无法让它发挥作用。搜索过滤功能似乎不适用于我的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);

}

}
4

1 回答 1

0

也许这就是你需要的:

mAdapter.setFilterQueryProvider(
    new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return mDb.query(TABLE_NAME, columns, COL_SanCat + " = ? AND " + COL_SandIngred + " = ?", new String[] { Type, "%" + constraint + "%" }, null, null, null);
        }
    }
);

编辑

我添加了关键字 return 以便它可以编译,抱歉。无论如何,如果用户输入“泡菜”,这应该会显示所有包含泡菜的三明治。

于 2012-04-14T00:04:27.717 回答