【问题标题】:android Search Listview SimpleCursorAdapter not workingandroid 搜索 Listview SimpleCursorAdapter 不工作
【发布时间】:2015-12-11 14:12:15
【问题描述】:

我正在尝试使用列表视图中的搜索视图进行自动搜索。 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;
            }
        });

【问题讨论】:

  • 如果你处理了这些事件,你应该返回true
  • 我很确定您应该将适配器和 OnQueryTextListener 设置为 SearchView 对象...不过我可能错了。
  • 所有你需要做的就是设置FilterQueryProvider

标签: android listview search android-arrayadapter


【解决方案1】:

我认为你应该每次在 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);
}}

【讨论】:

  • 如果可以的话,你能给个样品吗
  • 我不认为是这种情况,OP 说 onQueryTextChange 没有被调用。
【解决方案2】:

适合您的 searchView youtube,

如果您的 listAdapter 扩展为基本适配器,请关注此Link

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多