2013-03-26 55 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