android搜索Listview SimpleCursorAdapter无法正常工作

时间:2015-12-11 14:12:15

标签: android listview search android-arrayadapter

我正在尝试使用listview中的搜索视图进行自动搜索。 Listview连续填充多个项目。但是OnqueryListener没有触发任何东西。

以下是如何做的样本。

cursor = db.getAllRows();
        String[] storeFrom = new String[]{SQLiteHandler.KEY_ID, SQLiteHandler.KEY_NAME, SQLiteHandler.KEY_AGE, SQLiteHandler.KEY_GENDER, SQLiteHandler.KEY_HEIGHT, SQLiteHandler.KEY_WEIGHT};
        int[] toView = new int[]{R.id.tvId, R.id.tvName, R.id.tvAge, R.id.tvGender, R.id.tvHeight, R.id.tvWeight};
        cursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.customlist, cursor, storeFrom, toView, 0);
        personList = (ListView) findViewById(R.id.listView);
        personList.setAdapter(cursorAdapter);
        searchText = (SearchView) findViewById(R.id.search);
        searchText.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String text) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String text)
            {
                cursorAdapter.getFilter().filter(text);
                cursorAdapter.notifyDataSetChanged();
                return false;
            }
        });

2 个答案:

答案 0 :(得分:0)

我认为你应该每次在方法OnQueryTextChange中激活数据库查询,而不是调用NotifyDataSetChange()方法。同样的问题是我和我解决了这个问题。这是剪辑的代码;

@Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method

        if (!newText.trim().isEmpty()) {
Cursor cursor = dbHelper
                .searchByBrandText((newText != null ? newText: "@@@@"));

        if (cursor != null) {
if (cursor.moveToFirst()) {
                do {
                    brandOrCompositionNameArray.add(cursor.getString(0));

                } while (cursor.moveToNext());
}   customAdapterSuggestiveListView = new CustomAdapterSuggestiveListView(
                    SearchActivity.this, brandOrCompositionNameArray);
            brandOrcompositionNameList
                    .setAdapter(customAdapterSuggestiveListView);
}}

答案 1 :(得分:-1)

适用于您的searchView youtube

如果您的listAdapter已扩展为基准适配器,请按照此Link

进行操作