2012-08-01 2 views
0

내 안드로이드 애플 리케이션에서 아이템으로 이미지를 보여주는 눈금이 있습니다. 버튼을 클릭하면 항목의 위치가 변경됩니다. 커서 어댑터를 통해이 표를 채우는 중입니다. 이전에는 문제가 없었지만 이미지의 위치를 ​​변경하고 다시 그리드를 새로 고치려면 시간이 필요했습니다. 그래서 나는 진행 대화 상자를 구현하여 사용자는 무언가가 진행되고 있음을 이해할 수 있습니다.처리기가 안드로이드의 그리드보기를 새로 고침하지 않습니다

여기 내 코드가 있습니다.

내 처리기

public void handleMessage(Message msg) { 
switch (msg.what) { 
    case PROGRESS_DIALOG_HANDLER_ID: 
     progressDialog.dismiss(); //ProgressDialog 
     DBAdapter adapter = new DBAdapter(SplittedImageActivity.this); 
     adapter.open(); 
     Cursor cursor = adapter.getAllImages(); 
     adapter.close(); 
     startManagingCursor(cursor); 
     cursorAdapter.changeCursor(cursor); //My cursor adapter 
     gridView.setAdapter(cursorAdapter); 
     cursor.close(); 

내 온 클릭 방법

progressDialog = ProgressDialog.show(SplittedImageActivity.this, "", "Please wait..."); 
new Thread(){ 
     public void run() { 
     Random random = new Random(); 
     DBAdapter adapter = new DBAdapter(getApplicationContext()); 
     adapter.open(); 
     int childs = gridView.getChildCount(), oldPosition, newPotision; 
     newPotision = childs-1; 
     for(int i=0 ; i<childs ; i++){ 
      oldPosition = random.nextInt(m_cGridView.getChildCount()); 
      adapter.updatePosition(oldPosition, newPotision); //updates the position of the images in database 
      newPotision = oldPosition; 
     } 
     adapter.close(); 
     handler.sendEmptyMessage(PROGRESS_DIALOG_HANDLER_ID); 
    }; 
}.start(); 

문제 : 진행 대화 상자가 eprfectly 보여주는

, 위치 또한 데이터베이스에 변경됩니다. 하지만 gridview가 상쾌하지 않습니다. 모든 작업이 끝나면 진행 대화 상자가 사라지고 화면이 공백이됩니다.

내가 잘못하고있는 곳에서 도와 주시겠습니까?

답변

0

문제는 당신이 처리기에 새 어댑터를 할당하지만 주 스레드에서 NotifyDataSetChanged()를 호출하지 않는다고 생각합니다. 그 이유는 아마도 그리드를 업데이트하지 않는 이유 일 것입니다.

또한 AsyncTasks를 사용하지 않는 이유는 무엇입니까?

public class LoadAsyncTask extends AsyncTask<Void, Void, Boolean> { 
    Context context; 

    public LoadAsyncTask(Context context) { 
     this.context = context;   
    } 

    protected void onPreExecute() { 
    } 

    protected Boolean doInBackground(Void... v) { 
     DBAdapter adapter = new DBAdapter(context); 
     adapter.open(); 
     int childs = gridView.getChildCount(), oldPosition, newPotision; 
     newPotision = childs-1; 
     for(int i=0 ; i<childs ; i++){ 
        oldPosition = random.nextInt(m_cGridView.getChildCount()); 
        adapter.updatePosition(oldPosition, newPotision); //updates the position of the images in database 
        newPotision = oldPosition; 
     } 
     adapter.close(); 

     DBAdapter adapter = new DBAdapter(context); 
     adapter.open(); 
     Cursor cursor = adapter.getAllImages(); 
     adapter.close(); 
     startManagingCursor(cursor); 
     cursorAdapter.changeCursor(cursor); //My cursor adapter 
     gridView.setAdapter(cursorAdapter); 
     cursor.close();     
     return true; 
    } 

    protected void onPostExecute(Boolean success) { 
     if (success) { 
      //This will be executed on the main activity thread 
      cursorAdapter.notifyDataSetChanged(); 
     } else { 
      showError(); 
     } 
    } 

} 
관련 문제