使用SimpleAdapter过滤listView数据

时间:2013-07-19 09:08:23

标签: android android-listview filtering

我需要帮助才能让过滤工作。 ListView显示正确,但是当我在搜索字段中输入一个字母时,它会出现错误

java.lang.NullPointerException

并且申请结束。

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

    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

                    }
                });

2 个答案:

答案 0 :(得分:6)

全局设置ListAdapter适配器。我相信,这会奏效。的哈拉

答案 1 :(得分:1)

Luksprog提供的解决方案有效。