Android:无法在lazyloading列表视图上实现搜索

Android:无法在lazyloading列表视图上实现搜索,android,android-listview,android-asynctask,Android,Android Listview,Android Asynctask,我的应用程序中有一个自定义列表视图。我使用http请求获取JSON格式的数据。有一个异步任务用于加载listview的整个数据(图像除外) 在这个asyntask的onpostexecute()中,我调用另一个asynctask来获取listview的图像 现在,我正在尝试使用textchangedlistener在listview上实现一个搜索功能 问题是在后台加载图像时,我经常遇到空指针异常 谁能给我一个摆脱这个的方法吗。我目前正在做的是,在图像加载之前,我不会显示searchbox编辑文本

我的应用程序中有一个自定义列表视图。我使用http请求获取JSON格式的数据。有一个异步任务用于加载listview的整个数据(图像除外)

在这个asyntask的onpostexecute()中,我调用另一个asynctask来获取listview的图像

现在,我正在尝试使用textchangedlistener在listview上实现一个搜索功能

问题是在后台加载图像时,我经常遇到空指针异常

谁能给我一个摆脱这个的方法吗。我目前正在做的是,在图像加载之前,我不会显示searchbox编辑文本。但这会让用户等待很多,尤其是长列表

整个代码太长,无法发布。所以,我只是在这里粘贴它的简要概述。如果你还需要什么,一定要告诉我。我也会这么做

public class abcd extends Activity {
    ListView todlistview;
    List<HashMap<String, Object>> cnt = null;
    ArrayList<HashMap<String, Object>> searchResults; /

    .
    .




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


        @Override
        protected SimpleAdapter doInBackground(String... strJson) {
        .
        .
        .

            adapter = new SimpleAdapter(getBaseContext(), cnt, com.example.promoapp.R.layout.textviewfield, from, to);

            return adapter;
        }

        @Override
        protected void onPostExecute(SimpleAdapter adapter) {

            searchResults=new ArrayList<HashMap<String, Object>> (cnt);

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

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                .
        .


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

    /** AsyncTask to download and load an image in ListView */
    private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{

        Dialog dil;
        @Override
        protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {

            InputStream iStream=null;
                  .


                // Create a hashmap object to store image path and its position in the listview
                // Storing the path to the temporary image file
                // Storing the position of the image in the listview
                // Returning the HashMap object containing the image path and position

        }

        @Override
        protected void onPostExecute(HashMap<String, Object> result) {

        }



    }


    private TextWatcher filterTextWatcher = new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

        String searchString=Search.getText().toString();
                int textLength=searchString.length();               

                searchResults.clear();

                for(int i=0;i<cnt.size();i++)
                {
                    String placeName=cnt.get(i).get("country").toString();

                    if(textLength<=placeName.length()){
                        //compare the String in EditText with Names in the ArrayList
                        if(searchString.equalsIgnoreCase(placeName.substring(0,textLength))){
                            //Toast.makeText(getApplicationContext(),placeName,1).show();
                            searchResults.add(cnt.get(i));
                        }
                    }
                }

                String[] from = { "country","flag","details"};
                 int[] to = { com.example.promoapp.R.id.tv_country,com.example.promoapp.R.id.iv_flag,com.example.promoapp.R.id.tv_country_details};
                 adapter = new SimpleAdapter(getBaseContext(), searchResults, com.example.promoapp.R.layout.textviewfield, from, to);
                 todlistview.setAdapter(adapter);
                adapter.notifyDataSetChanged(); 
              }
     };


}
public类duo扩展活动{
列表视图到列表视图;
列表cnt=null;
ArrayList搜索结果/
.
.
/**AsyncTask解析json数据并加载ListView*/
私有类ListViewLoaderTask扩展异步任务{
@凌驾
受保护的SimpleAdapter doInBackground(字符串…strJson){
.
.
.
adapter=new simpledapter(getBaseContext(),cnt,com.example.promoapp.R.layout.textviewfield,from,to);
返回适配器;
}
@凌驾
受保护的void onPostExecute(SimpleAdapter适配器){
searchResults=新的ArrayList(cnt);
//设置listview的适配器
todlistview.setAdapter(适配器);

对于(int i=0;i首先,我不会在onPostExecute中执行像图像加载这样的繁重任务,因为这种方法在UI线程上工作。您只需使用ImageLoaderTask阻止UI。我会尝试在第一个异步任务中加载图像和其他所有内容。问题是您同时搜索和修改列表。尝试执行ListViewLoaderTask的图像加载onBackground添加您的logcat错误!!除非您专门将cnt列表和其中的哈希映射初始化为空对象,否则value object
object
将为空。不幸的是,几天前我计划发布此问题。我在代码,我收到了其他错误,现在没有日志。但我收到的错误是空指针异常。请求的索引x大小为y。我将尝试复制错误并发布logcatImageLoaderTask也是一个AsynTask。它不在UI上运行。@el_bhm从我看到的情况来看,它在UI上运行。我的任务与1相同)加载json(2)解析图像。是的,即使使用AsyncTask解析onPostExecute也会降低ui速度。问题是在搜索时修改了适配器,通常是onPostExecute()方法调用另一个asynctask,该任务在后台加载图像。因此,UI线程不会被阻止。保留两个asynctask的原因是图像是次要的,我们认为用户希望在稍后显示图像时先获取其他详细信息