如何在Lazy loading listview中添加搜索功能

时间:2015-06-19 05:12:30

标签: android json lazy-loading

我正在使用http请求获取JSON格式的数据,该格式也会加载图像。

我正在尝试实现onTextChanged功能,但每当我在EditText框中输入任何字母时,它总会崩溃。

/** AsyncTask to parse json data and load ListView */
    private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{

        JSONObject jObject;
        // Doing the parsing of xml data in a non-ui thread
        @Override
        protected SimpleAdapter doInBackground(String... strJson) {
            try{
                jObject = new JSONObject(strJson[0]);
                StoreJSONParser countryJsonParser = new StoreJSONParser();
                countryJsonParser.parse(jObject);
            }catch(Exception e){
                Log.d("JSON Exception1",e.toString());
            }

            // Instantiating json parser class
            StoreJSONParser countryJsonParser = new StoreJSONParser();

            // A list object to store the parsed countries list
            List<HashMap<String, Object>> countries = null;

            try{
                // Getting the parsed data as a List construct
                countries = countryJsonParser.parse(jObject);
            }catch(Exception e){
                Log.d("Exception",e.toString());
            }

            // Keys used in Hashmap
            String[] from = { "country","flag","details"};

            // Ids of views in listview_layout
            int[] to = { R.id.storename,R.id.icon,R.id.details};

            // Instantiating an adapter to store each items
            // R.layout.listview_layout defines the layout of each item
            adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.restaurant_list, from, to);
            Log.d("What is adapter ?", String.valueOf(adapter));
            return adapter;
        }

然后在postExecute上我实现了onTextChanged函数。

protected void onPostExecute(SimpleAdapter adapter) {

            // Setting adapter for the listview
            mListView.setAdapter(adapter);

            searchBox.addTextChangedListener(new TextWatcher() {

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

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

                }

                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    //ChooseCategory.this.adapter.getFilter().filter(arg0);
                }
            });

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in the listview
                imageLoaderTask.execute(hm);
            }
        }

我尝试在Log.d变量上执行countries,它显示正确的JSON数据。我从logcat得到的错误是

java.lang.NullPointerException
            at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
            at android.widget.AdapterView.checkFocus(AdapterView.java:713)
            at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:836)
            at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:6288)
            at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50)
            at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59)
            at android.widget.SimpleAdapter$SimpleFilter.publishResults(SimpleAdapter.java:383)
            at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

提前感谢您的帮助!

0 个答案:

没有答案