【问题标题】:Add filter to listview with two textview使用两个文本视图将过滤器添加到列表视图
【发布时间】:2015-03-24 13:52:17
【问题描述】:

我有一个自定义 ListView 和两个 TexView,我用下面的代码填充了一个 SimpleAdapter

adapter = new SimpleAdapter(this, list,
            R.layout.activity_lista_vehiculo,
            new String[] { "value1","value2" },
            new int[] {R.id.line_a, R.id.line_b});      
    setListAdapter( adapter );

如何从EditTextSearchView 对其进行过滤?

【问题讨论】:

    标签: java android android-intent android-activity android-listview


    【解决方案1】:

    SimpleAdapter 有一个内置的SimpleFilter,它会过滤带有前缀的ListView adapter 的内容。如果这是你想要的,你可以使用下面的代码来做过滤工作:

                yourEditText.addTextChangedListener(new TextWatcher() {
    
                    @Override
                    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                        // When user changed the Text
                        yourSimpleAdapter.getFilter().filter(cs); 
    
                    }
    
                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                            int arg3) {
    
                    }
    
                    @Override
                    public void afterTextChanged(Editable arg0) {
    
                    }
                });
    

    【讨论】:

    • 我喜欢它,我把代码放到 OnCreated 方法中,我有这个错误:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Proyect1/com.example.Proyect1 .ListViewStudient}: java.lang.NullPointerException
    • 它与您在上面输入的代码相同。你把“yourSimpleAdapter”放在哪里,我把我的适配器放在哪里,你把“yourEditText.addTextChangedListener(new TextWatcher()”放在哪里,我把txtsearch放在哪里......,
    • 可能 txtsearch 为空,请发布您的错误堆栈,可以在 logcat 中找到。
    猜你喜欢
    • 2016-10-02
    • 2011-06-08
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2014-09-29
    • 2022-12-28
    相关资源
    最近更新 更多