2012-02-18 3 views
0

비동기 작업이 완료되면 listivew를 어떻게 업데이트합니까? 다음은 샘플 코드이지만 listview는 업데이트되지 않습니다.asynctask 완료 후 listview 업데이트

class CallXML extends AsyncTask<Void, Void, Void> { 
    int gcid; 
    int scid; 

    public CallXML(int gid, int sid) { 
     // TODO Auto-generated constructor stub 
     gcid = gid; 
     scid = sid; 
    } 

    protected void onPreExecute() { 

    } 

    protected Void doInBackground(Void... arg0) { 
     // here goes the xml parsing.... 
     } 

     return null;    
    } 

    protected void onPostExecute(String result) { 
     Log.e("TAG", "In postExecute"); 


     Cursor cur3 = database2.query("Quote", qfield, null, null, null, null, null); 
     cur3.moveToFirst(); 

     do { 
     quotesarray.add(cur3.getString(2)); 

     } while (cur3.moveToNext()); 

     if(cur3 != null){ 
      cur3.close(); 
     } 

     QList.post(new Runnable() { 
      public void run() { 
       mAdapter = new CustomAdapter(); 
       mAdapter.notifyDataSetChanged(); 
       QList.setAdapter(mAdapter);  
      } 
     }); 


     if (helper2 != null) { 
      helper2.close(); 
     } 

     if (database2 != null) { 
      database2.close(); 
     } 

    } 
} 

편집 :

Acutally onPostExecute가 실행되지 않습니다 why..This 내가 당신은 당신의 코드에서 어댑터에 문자열 항목을 제공하지 않는 asynctask new CallXML(gcid, scid).execute();

답변

2

또한 onPostExecute이 주 스레드에 있으므로 데이터베이스 쿼리를 수행해서는 안됩니다. 대신 데이터를 doInBackground으로 가져 와서 거기에서 최종 컬렉션을 반환하십시오.

onPostExecute은 UI 업데이트 및 결과 수집을 통한 어댑터 업데이트에 사용할 수 있습니다.

편집 : 당신이 메인 루프에 있기 때문에 실행 가능한

QList.post(new Runnable() { 
       public void run() { 
        //mAdapter.notifyDataSetChanged(); 
        QList.setAdapter(mAdapter);  
       } 
      }); 

가 필요하지 않은 게시.

2

를 호출하는 방법입니다. 어댑터를 설정하면 목록에 데이터를 자동으로로드하므로 어댑터를 목록에 설정할 때 notifyDataSetChanged를 호출 할 필요가 없습니다. 아마 당신은이 방법으로 그것을 시도 :

protected void onPostExecute(String result) { 
     Log.e("TAG", "In postExecute"); 

     Cursor cur3 = database2.query("Quote", qfield, null, null, null, null, null); 
     cur3.moveToFirst(); 

     mAdapter = new CustomAdapter(); 

     do { 
     mAdapter.add(cur3.getString(2)); 

     } while (cur3.moveToNext()); 

     if(cur3 != null){ 
      cur3.close(); 
     } 

     QList.post(new Runnable() { 
      public void run() { 
       //mAdapter.notifyDataSetChanged(); 
       QList.setAdapter(mAdapter);  
      } 
     }); 


     if (helper2 != null) { 
      helper2.close(); 
     } 

     if (database2 != null) { 
      database2.close(); 
     } 

    } 
0

u는 오류를 얻었나요? 그렇다면 게시하십시오. 그렇지 않으면 데이터베이스에서 가져온 데이터의 크기를 확인하고 listview를 새로 고치려면 listview.invalidateViews()를 호출하면 목록보기가 새로 고쳐지고 listview에 새 데이터가 설정됩니다.

관련 문제