【问题标题】:How to search within ListView with custom adapter如何使用自定义适配器在 ListView 中搜索
【发布时间】:2013-12-28 16:17:36
【问题描述】:

我查看了以下网站:ListView Example

其中描述了如何在使用默认适配器的列表视图中实现搜索功能,并且工作正常。

如何修改它以便我可以将其用于我的列表视图的自定义适配器?

部分代码为:

        dataList = (ListView) findViewById(R.id.lvFiles);
        tvQuote = (TextView) findViewById(R.id.tvDisplay);
        tvQuote.setTypeface(Typeface.createFromAsset(MainActivity.this.getAssets(), "fonts/roboto.ttf"));

        for (int y=0; y<strNamesOfAllah.length;y++) {
            name = strNamesOfAllah[y];
            meaning = strMeaning[y];
            rowsArray.add(new SetRows(R.drawable.icon, name, meaning));
        }
        adapter = new SetRowsCustomAdapter(MainActivity.this, R.layout.customlist, rowsArray);
        dataList.setAdapter(adapter);
        dataList.setClickable(true);

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    您需要覆盖适配器内部的 getFilter 并返回您创建的新 customFilter 对象。看到这个答案:No results with custom ArrayAdapter Filter

    编辑:

    @Override
        public Filter getFilter() {
            if(customFilter == null){
                customFilter = new CustomFilter();
            }
            return customFilter;
        }
    

    【讨论】:

    • 我在这里找到了一个很好的例子:9android.net/add-search-function-to-custom-listview 但我不确定如何修改 getFilter() 以使用我的基本适配器 (pastebin.com/zSsv7862)
    • 只需覆盖 getFilter 并创建一个新的,如果不存在的话,请参阅我上面的编辑
    • 这是完整的代码吗?我找到了一个自定义过滤器函数,但我对一些变量感到困惑。可以看看吗?
    • getFilter 代码应该放在您的适配器中,并且您应该创建自己的 CustomFilter 类,类似于我在上面发布的链接中的类。
    • 这帮助我完成了代码:stackoverflow.com/questions/20524417/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多