代码之家  ›  专栏  ›  技术社区  ›  CHarris

如何使适配器大小正确反映在onQueryTextChange中?

  •  0
  • CHarris  · 技术社区  · 8 年前

    当用户键入 searchbox onQueryTextChange 因此文本被过滤,并且:

    1) 一个 "No Results" 应显示文本框-如果没有键入与 items 在我的名单上。

    2) 匹配 项目 recyclerView 。

    落后了一步。

    项目 item 以“p”开头

    搜索框 回收服务 -但我得到的吐司是“mAdapter尺码是:13”。13是 , mAdapter 大小应该只有1。

    当我输入“pl”时 toast 对于 正确,显示1。

    当我输入“plk”时 是错误的,它仍然显示1-应该说, "mAdapter is 0, no item results" 回收服务 “没有结果” 文本框。

    只有当我输入下一个字符时 "plke" ,我明白了 mAdapter为0,没有项目结果 “没有结果” 文本框。

    这是我的代码:

    @Override
          public boolean onQueryTextChange(String query) {
    
              //items is the complete list of the category names available to the logged-in user
              items = new Gson().fromJson(the_response, new TypeToken<List<Category>>() {
              }.getType());
    
              //clear the list every time a key is pressed
              categoryList.clear();
    
              //was getting NullPointerException so put this here...
              if (items != null && items.size() > 0) {
                //adding categories to category list
                categoryList.addAll(items);
              }
    
              mAdapter.getFilter().filter(query);
    
              recyclerView.setAdapter(mAdapter);
    
              if (mAdapter.getItemCount() < 1) {
    
                //if there's nothing to show, hide recyclerView...
                recyclerView.setVisibility(View.GONE);
    
                //and show the "No Results" textbox
                noResultsFoundView.setVisibility(View.VISIBLE);
    
                Toast.makeText(getApplicationContext(), "mAdapter is 0, no item results", Toast.LENGTH_SHORT).show();
    
              }
    
            if (mAdapter.getItemCount() >= 1) {
    
                recyclerView.setVisibility(View.VISIBLE);
                noResultsFoundView.setVisibility(View.GONE);
    
                //if there ARE category results for what is typed, with each key press...
                Toast.makeText(getApplicationContext(), "mAdapter size is:" + mAdapter.getItemCount(), Toast.LENGTH_SHORT).show();
    
    
              }
    
              //if the searchView is empty
              if (searchView.getQuery().length() == 0) {
    
                //show the logged-in users reviews, not the searched categories
                recyclerView.setAdapter(pAdapter);
                pAdapter.notifyDataSetChanged();
    
                //show the recyclerview, hide the noResults textview
                recyclerView.setVisibility(View.VISIBLE);
                noResultsFoundView.setVisibility(View.GONE);
    
              }
    
              // refreshing recycler view
              mAdapter.notifyDataSetChanged();
    
    
            return false;
    
          }
    
    0 回复  |  直到 8 年前
    推荐文章