2013-03-26 3 views
0

내 앱에 맞춤 목록 뷰가 있습니다. HTTP 요청을 사용하여 JSON 형식의 데이터를 가져옵니다. 리스트 뷰에 대한 전체 데이터 (이미지 제외)를로드하는 asynctask가 있습니다.Android : lazyloading listview에서 검색을 구현할 수 없습니다.

이 asyntask의 onpostexecute()에서 listview에 대한 이미지를 가져 오기 위해 다른 asynctask를 호출합니다.

가 이제 문제는 이미지가 배경에로드하기로하는 textchangedlistener

를 사용하여 목록보기에 검색 기능을 구현하기 위해 노력하고, 나는 종종 널 포인터 예외로 실행합니다.

아무도 나에게 이것을 없애는 방법을 제안 할 수 있습니까? 현재 내가하고있는 일은 이미지가로드 될 때까지 searchbox edittext를 표시하지 않을 것입니다. 그러나 이로 인해 사용자는 특히 긴 목록의 경우 많이 기다리게됩니다.

전체 코드가 너무 길기 때문에 게시 할 수 없습니다. 그래서 여기에 간단한 개요를 붙이고 있습니다. 네가 다른 것을 필요로하면 알려줘. 나는 그렇게 할 것이다. 이 방법은 UI 스레드에서 작동하기 때문에

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

+0

비어있는 객체에 cnt List 및 HashMaps를 구체적으로 초기화하지 않는 한 값 객체'Object'는 null입니다. –

+0

불행히도이 질문을 며칠 전 게시 할 계획이었습니다. 나는 코드에서 다른 많은 변화를 만들었고, 다른 에러를 얻었고 지금은 로그캣을 가지고 있지 않습니다. 그러나 내가 가진 오류는 null 포인터 예외였습니다. 색인 x는 y 크기로 요청했습니다. 오류를 복제하고 logcat을 게시하려고 시도합니다. – ambit

답변

0

첫째, 나는 에 onPostExecute을 이미지 로딩과 같은 무거운 작업을 수행하지 않을 것입니다. ImageLoaderTask를 사용하여 UI를 차단하면됩니다. 첫 번째 AsyncTask에서 다른 모든 것들과 함께 이미지를로드하려고합니다. 문제는 동시에 목록을 검색하고 수정하는 것입니다. 이미지 로딩을 시도하십시오 onBackground ListViewLoaderTask의

+0

ImageLoaderTask도 AsynTask입니다. 그것은 UI에서 실행되지 않습니다. –

+0

@el_bhm 본 것부터 UI에서 실행됩니다. 나는 1) load json 2) parse image와 같은 작업을했다. 그리고 예, AsyncTask를 사용하여 onPostExecute를 구문 분석하면 UI가 느려집니다. 문제는 일반적으로 검색 할 때 어댑터를 수정한다는 것입니다. – Roman

+0

onpostexecute() 메서드는 백그라운드에서 이미지를로드하는 작업을 수행하는 다른 asynctask를 호출합니다. 따라서 UI 스레드는 차단되지 않습니다. 두 개의 asynctask를 유지해야하는 이유는 이미지가 2 차적이며 사용자가 다른 세부 사항을 먼저 얻고 싶을 때 나중에 이미지가 표시되는 것입니다. – ambit

관련 문제