带有ListView的EditText

时间:2012-04-13 23:06:24

标签: android listview simplecursoradapter

我知道这已经多次被介绍过了,但我无法让它发挥作用。我的ListView似乎无法正常使用搜索过滤功能。任何人都可以帮我理解我的代码中有什么问题吗?结果从我的数据库中拉出来,但即使我在EditText字段中输入文本后也未过滤。我尝试使用Watcher测试关键更改的EditText,除了我的列表过滤之外它工作正常。

非常赞赏。

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);

}

}

1 个答案:

答案 0 :(得分: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,因此它可能会编译,抱歉。无论如何,如果用户输入“泡菜”,这应该显示所有包含泡菜的三明治。