使用 SimpleAdapter 过滤 listView 数据

2024-03-06

我需要帮助才能使过滤发挥作用。 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

                    }
                });

全局设置您的 ListAdapter 适配器。我确信,这会起作用。Kamal

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 SimpleAdapter 过滤 listView 数据 的相关文章

随机推荐