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

如何对由SimpleCursorAdapter支持的Android ListView进行文本筛选?

  •  18
  • CodeFusionMobile  · 技术社区  · 15 年前

    我有一个由SimpleCursorAdapter支持的ListView。

    我希望能够像过滤联系人列表一样过滤列表,只需键入即可,我遇到了textFilterEnabled()


    3 回复  |  直到 11 年前
        1
  •  7
  •   Community CDub    8 年前

    这个 setTextFilterEnabled() 什么 在你的 Cursor

    android-developers thread

    事实上,前几天问了一个很好的问题,这实际上与你的问题非常相似;虽然它最初是询问在设备上没有物理键盘时如何处理过滤:

        2
  •  24
  •   Mark B    14 年前

    m_Adapter.setFilterQueryProvider(new FilterQueryProvider() {
    
      public Cursor runQuery(CharSequence constraint) {
        Log.d(LOG_TAG, "runQuery constraint:"+constraint);
        //uri, projection, and sortOrder might be the same as previous
        //but you might want a new selection, based on your filter content (constraint)
        Cursor cur = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
        return cur; //now your adapter will have the new filtered content
      }
    
    });
    

    添加约束时(例如通过使用TextView),必须过滤适配器:

    public void onTextChanged(CharSequence s, int start, int before, int count) {
      Log.d(LOG_TAG, "Filter:"+s);
      if (m_slvAdapter!=null) {
        m_Adapter.getFilter().filter(s);
      }
    }
    

    希望这有帮助。在接下来的几天里,我将尝试写一篇完整的文章,包括源代码。

        3
  •  0
  •   Ben H    14 年前

    我觉得这篇文章很有帮助 http://androidcookbook.oreilly.com/Recipe.seam;jsessionid=CE37400B3E545937B70BE2E9F94E78BB?recipeId=404

    基本上,你 setTextFilterEnabled(true) setStringConversionColumn() setFilterQueryProvider() SimpleCursorAdapter .