使用SimpleAdapter过滤listView数据 [英] Filtering listView data using SimpleAdapter

查看:119
本文介绍了使用SimpleAdapter过滤listView数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助才能进行过滤. ListView可以正确显示,但是当我在搜索字段中输入字母时会出现错误

I would need help to get filtering to work. ListView is displayed correctly, but when I enter a letter in the search field it gives an error

java.lang.NullPointerException

应用程序关闭.

这是我认为必须修复的活动代码段,但不知道如何:

Here is the code snippet from my activity that I think I have to fix, but don't know how:

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all albums
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                ListAdapter adapter = new SimpleAdapter(
                        WineSearchActivity_2.this, albumsList,
                        R.layout.activity_search_list_of_wines, new String[] {
                                TAG_wine_id, TAG_wine_name,
                                TAG_wine_country_id,
                                TAG_wine_country_flag_pic }, new int[] {
                                R.id.wine_id, R.id.wine_name,
                                R.id.wine_country_id, R.id.imageView2 });
                ListView lv = getListView();
                LayoutInflater inflater = getLayoutInflater();
                View header = inflater.inflate(R.layout.header,
                        (ViewGroup) findViewById(R.id.LinearLayoutHeader));
                View search_box = inflater.inflate(R.layout.search_box,
                        (ViewGroup) findViewById(R.id.RelativeLayoutSearchBox));
                lv.addHeaderView(header, null, false);
                lv.addHeaderView(search_box, null, false);

                // updating listview
                setListAdapter(adapter);

                inputSearch = (EditText) findViewById(R.id.inputSearch);

                inputSearch.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                        // When user changed the Text
                        ((SimpleAdapter)WineSearchActivity_2.this.adapter).getFilter().filter(cs); 

                    }

                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                            int arg3) {
                        // TODO Auto-generated method stub


                    }

                    @Override
                    public void afterTextChanged(Editable arg0) {
                        // TODO Auto-generated method stub

                    }
                });

推荐答案

Luksprog提供的解决方案有效.

The solution provided by Luksprog worked.

这篇关于使用SimpleAdapter过滤listView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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