Filtering listView data using SimpleAdapter

荒凉一梦 提交于 2019-12-14 01:41:58

问题


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

and application closes.

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

                    }
                });

回答1:


Set your ListAdapter adapter globally. I am sure , this will work. Kamal




回答2:


The solution provided by Luksprog worked.



来源:https://stackoverflow.com/questions/17742206/filtering-listview-data-using-simpleadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!