2013-03-26 71 views
0

我有我的应用程序中的自定义列表视图。我使用http请求以JSON格式获取数据。有一个asynctask加载列表视图的全部数据(图像除外)。Android:无法实现懒惰列表视图上的搜索

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

现在我想实现在使用textchangedlistener

的问题是与背景图像得到加载列表视图搜索功能,我经常碰到空指针异常。

任何人都可以建议我一种方法来摆脱这一点。我现在正在做的是,我没有显示搜索框edittext,直到加载图像。但是这会让用户等待很长时间,特别是对于长列表。

整个代码太长,无法发布。所以,我只是在这里粘贴它的简要提纲。如果你需要其他东西,请告诉我。我也会这样做。

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(); 
       } 
    }; 


} 
+0

添加您的logcat错误! – Janmejoy 2013-03-26 15:00:56

+0

除非您特别初始化cnt List和HashMaps以清空对象,否则value对象'Object'将为空。 – 2013-03-26 15:31:38

+0

不幸的是,我打算几天后发布这个问题。我在代码中做了许多其他更改,现在正在发生其他错误并且没有logcat。 但我得到的错误是空指针异常..索引x与y的大小请求..我会尝试复制错误并发布logcat – ambit 2013-03-26 15:40:01

回答

0

首先,我不会执行任务重像图像加载onPostExecute,因为该方法是基于UI线程。您只需使用ImageLoaderTask阻止UI。我会尝试在第一个AsyncTask中加载所有其他图像。问题是您正在同时搜索和修改列表。尝试做图像加载OnBackground ListViewLoaderTask

+0

ImageLoaderTask也是一个AsynTask。它不会在UI上运行。 – 2013-03-26 15:24:48

+0

@el_bhm从我所看到的,它在UI上运行。我有同样的任务1)加载json 2)解析图像。是的,解析onPostExecute,即使使用AsyncTask也会减慢UI。问题在于你在搜索时修改适配器,通常 – Roman 2013-03-26 15:27:48

+0

onpostexecute()方法调用另一个asynctask,它执行在后台加载图像的任务。所以,UI线程不会被阻止。而保持两个asynctask的原因是图像是次要的,我们认为用户希望首先获得其他细节,而图像稍后显示 – ambit 2013-03-26 15:34:05