2012-03-18 147 views
0

我有一個android應用程序,它的主要活動,數據從sqlite數據庫改編,並顯示在列表視圖。我試圖使用進度對話框在從數據庫獲取數據期間向用戶顯示「加載」消息。但對話不會消失。ProgressDialog不消失在Android應用程序

下面是代碼:

public class BirthdayAlarmActivity extends ListActivity { 

    List<BirthdayContact> listofAvailableBirthdays; 

    ProgressDialog pDialog; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.birthday_list); 


     listofAvailableBirthdays=new ArrayList<BirthdayContact>(); 

     ReinitializeList(); 
} 

@Override 
protected void onResume() { 

     ReinitializeList(); 
} 


void ReinitializeList() 
    { 

     new LoadListView().execute(); 

      if(listofAvailableBirthdays.size()>0) 
     { 
      //get ready the adapter 
      ArrayAdapter<BirthdayContact> ara= 
        new     MyArrayAdapter(BirthdayAlarmActivity.this,listofAvailableBirthdays); 

      //set the adapter 
      setListAdapter(ara); 
      } 

} 

public class LoadListView extends AsyncTask<Void, Void, Void> 
{ 

     //ProgressDialog pDialog; 

     @Override 
     protected void onPreExecute() { 
      // Showing progress dialog before sending http request 
      pDialog = new ProgressDialog(
        BirthdayAlarmActivity.this); 
      pDialog.setMessage("Please wait.."); 
      pDialog.setIndeterminate(true); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     protected Void doInBackground(Void... unused) { 
      runOnUiThread(new Runnable() { 
       public void run() { 
        // increment current page 
        listofAvailableBirthdays.clear(); 

        listofAvailableBirthdays=BirthdayHandler.GetTenBirthDays(BirthdayAlarmActivity.this); 
       } 
      }); 
      return (null); 
     }  

     protected void onPostExecute(Void unused) { 
      // closing progress dialog 
      pDialog.dismiss(); 
     } 

    } 
+0

如果您在UI線程上運行doInBackground的所有代碼,那麼使用AsyncTask有什麼意義? – MByD 2012-03-18 10:23:26

+0

我是多線程的概念的新手,並從這個示例中學習它[http://www.androidhive.info/2012/03/android-listview-with-load-more-button/]。我該怎麼辦 ? – 2012-03-18 10:30:45

回答

2

我不熟悉您正在使用的技術,但我會分享什麼工作對我來說:

final ProgressDialog progress = ProgressDialog.show(activity, "", 
    activity.getString(R.string.please_wait), true); 

new Thread(new Runnable() { 
    @Override 
    public void run() 
    { 
    try { 
     --- network activity to retrieve information --- 
    } 
    finally { 
     activity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() 
     { 
      if ((progress != null) && progress.isShowing()) 
      progress.dismiss(); 
     } 
     }); 
    } 
    } 
}).start(); 
+0

你的意思是我不需要在活動類體中聲明'ProgressDialog'? – 2012-03-18 10:54:02

+0

我不認爲你必須這樣做,但這取決於你。 – dldnh 2012-03-18 11:02:56

+0

它引發「RunOnUiThread(新的Runnable(){})未定義類型新的Runnable(){}」 – 2012-03-18 11:14:55

1

我覺得問題這裏是你指的是你的pDialog,並試圖用與你聲明的類別不同的​​類別創建它。

你應該嘗試使用showDialog,dismissDialogonCreateDialog添加一個抽象層的方法,並且在正確的類/線程中調用對話框。您可以使用處理程序以及替代方法。

嘗試這樣:

public class BirthdayAlarmActivity extends ListActivity { 

List<BirthdayContact> listofAvailableBirthdays; 

ProgressDialog pDialog; 
static final int LOADING_DIALOG = 1; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.birthday_list); 


    listofAvailableBirthdays=new ArrayList<BirthdayContact>(); 

    ReinitializeList(); 
} 

@Override 
protected void onResume() { 

    ReinitializeList(); 
} 


void ReinitializeList() 
{ 

    new LoadListView().execute(); 

     if(listofAvailableBirthdays.size()>0) 
    { 
     //get ready the adapter 
     ArrayAdapter<BirthdayContact> ara= 
new MyArrayAdapter(BirthdayAlarmActivity.this,listofAvailableBirthdays); 

     //set the adapter 
     setListAdapter(ara); 
     } 

} 

@Override 
protected Dialog onCreateDialog(int id) 
{ 
    switch(id) 
    { 
    case LOADING_DIALOG: 
    // Showing progress dialog before sending http request 
     pDialog = new ProgressDialog(
       BirthdayAlarmActivity.this); 
     pDialog.setMessage("Please wait.."); 
     pDialog.setIndeterminate(true); 
     pDialog.setCancelable(false); 
     return pDialog; 

    } 
} 


public class LoadListView extends AsyncTask<Void, Void, Void> 
{ 


    @Override 
    protected void onPreExecute() { 
     showDialog(LOADING_DIALOG); 
    } 

    protected Void doInBackground(Void... unused) { 
     runOnUiThread(new Runnable() { 
      public void run() { 
       // increment current page 
       listofAvailableBirthdays.clear(); 

       listofAvailableBirthdays=BirthdayHandler.GetTenBirthDays(BirthdayAlarmActivity.this); 
      } 
     }); 
     return (null); 
    }  

    protected void onPostExecute(Void unused) { 
     dismissDialog(LOADING_DIALOG); 
    } 

} 

如果失敗,你應該看看INT通過Handler類使用的消息。

+0

謝謝。我測試了你的感覺,而在Hanlder當我想更新列表視圖適配器時,它確實拋出錯誤,並告訴我我不能從另一個線程更新它。 – 2012-03-18 14:10:53