如何在ListView中搜索与自定义适配器 [英] How to search within ListView with custom adapter

查看:90
本文介绍了如何在ListView中搜索与自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了看下面的网站: <强> ListView控件实例

,它描述了如何实现在ListView它使用默认的适配器搜索功能,它工作正常。

我怎么可以修改它,所以我可以用相同的自定义适配器为我的列表视图?

偏code是:

 的DataList =(ListView控件)findViewById(R.id.lvFiles);
        tvQuote =(TextView中)findViewById(R.id.tvDisplay);
        tvQuote.setTypeface(Typeface.createFromAsset(MainActivity.this.getAssets(),字体/ roboto.ttf));

        对于(INT Y = 0; Y&LT; strNamesOfAllah.length; Y ++){
            名称= strNamesOfAllah [Y]
            意思= strMeaning [Y]
            rowsArray.add(新SetRows(R.drawable.icon,名字,意思));
        }
        适配器=新SetRowsCustomAdapter(MainActivity.this,R.layout.customlist,rowsArray);
        dataList.setAdapter(适配器);
        dataList.setClickable(真正的);
 

解决方案

您需要重写用getFilter适配器内,并返回您创建一个新的customFilter对象。看到这个答案:<一href="http://stackoverflow.com/questions/8255322/no-results-with-custom-arrayadapter-filter/8258457#8258457">No自定义ArrayAdapter过滤器结果

编辑:

  @覆盖
    公共过滤用getFilter(){
        如果(customFilter == NULL){
            customFilter =新CustomFilter();
        }
        返回customFilter;
    }
 

I looked at the following site: ListView Example

Which describes how to implement a search function in a listview which uses the default adapter and it works fine.

How can I modify it so I can use the same for a Custom Adapter for my listview?

Partial code is:

        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);

解决方案

You need to override getFilter inside of your adapter and return a new customFilter object that you create. See this answer: No results with custom ArrayAdapter Filter

Edit:

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

这篇关于如何在ListView中搜索与自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆