2014-04-11 2 views
0

비동기 작업을 사용하여 사용자 지정 목록보기를로드하고 있습니다. 로딩 diaglog를 표시합니다. doInBackground을 실행하여 필요한 모든 것을 수집하십시오. 비동기 작업이 완료 될 때 업데이트되지, 점차적으로 업데이트 할 목록보기 싶습니다 제외비동기 작업 내에서 사용자 지정 어댑터 업데이트

adapter = new ImageAdapter(this, pix, paths); 
lstView.setAdapter(adapter); 

확인 일 : 는 다음 onPostExecute 나는 이것이 ListView 모든 것을 보여 않습니다. 그래서 대화 상자가 없어야합니다. 사용자는 ListView가 행 단위로 채워지는 것을보아야합니다.

여기 내 doInBackground입니다. ListView가 즉시 업데이트되도록하려면 무엇을 변경해야합니까?

@Override 
protected Boolean doInBackground(DbxFileSystem... params) { 

    try { 
     DbxFileSystem fileSystem = params[0]; 


     for (DbxFileInfo fileInfo: fileSystem.listFolder(currentPath)) { 
      String filename = fileInfo.path.getName(); 
      try{ 
       if(!fileInfo.isFolder) 
       { 
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
        pix.add(image); 

        paths.add(fileInfo.path); 
       } 
       else 
       { 
        //must be a folder if it has no thumb, so add folder icon 
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dbfolder); 
        pix.add(image); 
        paths.add(fileInfo.path); 
       } 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      System.gc(); 
     } 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } finally { 
     loadingDialog.dismiss(); 
    } 
    return true; 
} 

다시 호출하여 어댑터를 ListView에 다시 바인딩해야합니까?

lstView.setAdapter(adapter); 

또는 한 번 바인딩하면 어댑터에 대한 모든 업데이트가 목록보기에 표시됩니까?

[편집] 여기 notifyDataSetChange D를 사용하려고하는 새로운 코드는 여기에

어댑터를 생성에서 onCreate가,의를 설정한다 (하지만 어떤 이유로 내 사용자의 ListView에 표시되는 것이없는)입니다 초기 값 (빈 배열)이 표시되고 어댑터가 listview에 바인드됩니다. 에서

lstView = (ListView) findViewById(R.id.lstView); 
     adapter = new ImageAdapter(this, pix, paths); 
     lstView.setAdapter(adapter); 

내 AsyncTask를의 doInBackground, 내가 publishProgress를 호출하고 (1); 루프. 나는 '1'의 값을 보내고 있지만 사용되지는 않습니다.

publishProgress(1); 

하면 마지막, 나는 notifyDataSetChanged를 사용하는 onProgressUpdate을 가지고 있지만, 그것은 아무것도 아니다. 그것은 목록보기를 업데이 트하지 않습니다.

notifyDataSetChanged(); 

은 (기본 데이터가 변경된 데이터 세트를 반영하는 어떤보기 자체를 새로해야한다는 부착 관찰자에게 통지 : 당신이 당신의 어댑터에 데이터를 추가 할 때

@Override 
    protected void onProgressUpdate(Integer...progress) { 

     adapter.notifyDataSetChanged(); 
     super.onProgressUpdate(progress); 
    } 

답변

2

어댑터를 리 바인드 할 필요가 없습니다. 단순히 "adapter.notifyDataSetChanged()"를 호출하여 데이터가 변경되었음을 알립니다. 또한 doInBackground()에서 UI 또는 다른 UI 작업을 호출하면 안됩니다. doInBackground에서 모든 것을 검색하여 결과로 반환 한 다음 onPostExecute에서 notifyDataSetChanged를 호출하거나 publishProgress()를 사용하고 onProgressUpdate()에서 notifyDataSetChanged를 호출합니다.

당신이 원하기 때문에 "의 ListView는 즉시 업데이트하지 말하세요"사용 publishProgress()와 onProgressUpdate()이 같은 :

@Override 
protected Boolean doInBackground(DbxFileSystem... params) { 

    try { 
     DbxFileSystem fileSystem = params[0]; 


     for (DbxFileInfo fileInfo: fileSystem.listFolder(currentPath)) { 
      String filename = fileInfo.path.getName(); 
      try{ 
       if(!fileInfo.isFolder) 
       { 
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
        pix.add(image); 

        paths.add(fileInfo.path); 
        publishProgress(1); 
       } 
       else 
       { 
        //must be a folder if it has no thumb, so add folder icon 
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dbfolder); 
        pix.add(image); 
        paths.add(fileInfo.path); 
        publishProgress(1); 
       } 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      System.gc(); 
     } 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } finally { 
     // loadingDialog.dismiss(); 

    } 
    return true; 
} 

} 
@Override 
    protected void onProgressUpdate(Integer... progress) { 
     //check log if paths and pix are updated 
     //Log.d("paths_size", String.valueOf(paths.size()); 
     //Log.d("pix_size", String.valueOf(pix.size()); 
     adapter.notifyDataSetChanged(); 
     lstView.requestLayout(); 
     //if notifyDataSetChanged() fails try the ff: 
     //lstView.setAdapter(null); 
     //lstView.setAdapter(new ImageAdapter(this, pix, paths); 
     super.onProgressUpdate(progress); 
    } 
+0

에서 코드를 업데이트 할 것이므로이 코드는 어디에 두어야합니까? 현재 post에 실행 중입니다 : adapter = new ImageAdapter (this, pix, paths); lstView.setAdapter (어댑터); – user3437721

+0

onPostExecute에서 해당 코드를 다시 호출 할 필요가 없습니다. onCreate() 메서드에서 한 번만 호출하고 완료 한 것처럼 adapter.notifyDataSetChanged()를 호출하면됩니다. –

+0

고마워요. 지금 시험해보고 그 작품이 있는지 살펴 보겠습니다. – user3437721

1

, 당신은 전화를해야합니다.)

+0

으로 notifyDataSetChanged();를 호출 할 수 있습니다. 내 맞춤 목록에 아무 것도 표시되지 않는다는 점에서 작동하지 않습니다.코드를 살펴보면 어댑터가 새로운 값으로 업데이트됩니다. 하지만 notifyDataSetChanged() 호출; 아무것도하지 않는다. 메인 포스트 – user3437721

0

전화 adapter.notifyDataSetChanged();합니다. 요소를 추가하는 대신 목록을 다시로드하는 경우 list.clear(); list.add(newList); adapter.notifyDataSetChanged();

관련 문제