2013-01-18 89 views
-2

我正在嘗試使用ProgressDialog。當我運行我的應用程序時,進度對話框顯示並在1秒後消失。我想表明它在我的程序完成。這裏是我的代碼:ProgressDialog Android

public class MainActivity extends Activity { 
android.view.View.OnClickListener mSearchListenerListener; 
private ProgressDialog dialog; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new YourCustomAsyncTask().execute(new String[] {null, null}); 

     } 



    private class YourCustomAsyncTask extends AsyncTask <String, Void, Void> { 

     protected void onPreExecute() { 
      dialog = new ProgressDialog(MainActivity.this); 
      dialog.setMessage("Loading...."); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(true); 
      dialog.show(); //Maybe you should call it in ruinOnUIThread in doInBackGround as suggested from a previous answer 
     } 

     protected void doInBackground(String strings) { 
      try { 

      // search(strings[0], string[1]); 

       runOnUiThread(new Runnable() { 
       public void run() { 
        // updateMapWithResult(); //Or call it onPostExecute before progressDialog's dismiss. I believe this method updates the UI so it should run on UI thread 
       } 
       }); 

      } catch(Exception e) { 
      } 


     } 

    @Override 
    protected void onPostExecute(Void params) { 
     dialog.dismiss(); 
     //result 

    } 

    @Override 
    protected Void doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 
} 

更新問題:

 @Override 
    public void onCreate(SQLiteDatabase db) { 
     mDatabase = db; 





      Log.i("PATH",""+mDatabase.getPath()); 



     mDatabase.execSQL(FTS_TABLE_CREATE); 





     loadDictionary(); 
    } 

    /** 
    * Starts a thread to load the database table with words 
    */ 
    private void loadDictionary() { 
     new Thread(new Runnable() { 
      public void run() { 
       try { 
        loadWords(); 
       } catch (IOException e) { 
        throw new RuntimeException(e); 
       } 
      } 
     }).start(); 
    } 

    private void loadWords() throws IOException { 
     Log.d(TAG, "Loading words..."); 


     for(int i=0;i<=25;i++) 

      { //***// 


     final Resources resources = mHelperContext.getResources(); 
     InputStream inputStream = resources.openRawResource(raw_textFiles[i]); 
     //InputStream inputStream = resources.openRawResource(R.raw.definitions); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 

     try { 





      StringBuilder sb = new StringBuilder(); 
      while ((word = reader.readLine()) != null) 
      { 
       sb.append(word); 
      // Log.i("WORD in Parser", ""+word); 
      } 


      String contents = sb.toString(); 
      StringTokenizer st = new StringTokenizer(contents, "||"); 
      while (st.hasMoreElements()) { 
       String row = st.nextElement().toString(); 

       String title = row.substring(0, row.indexOf("$$$")); 
       String desc = row.substring(row.indexOf("$$$") + 3); 
       // Log.i("Strings in Database",""+title+""+desc); 
       long id = addWord(title,desc); 

       if (id < 0) { 
        Log.e(TAG, "unable to add word: " + title); 
       } 
      } 

     } finally { 
      reader.close(); 
     } 

     } 

     Log.d(TAG, "DONE loading words."); 
    } 

我想告訴ProgressDialogue箱,直到所有的話都沒有輸入到數據庫中。該代碼位於內部,它擴展了SQLITEHELPER。所以如何在內部類中使用ProgressDialogue並在後臺運行我的addWords()方法。

回答

1

你不能有這個

runOnUiThread(new Runnable() { 
       public void run() { 
        // updateMapWithResult(); //Or call it onPostExecute before progressDialog's dismiss. I believe this method updates the UI so it should run on UI thread 
       } 
       }); 

在doInBackground()。

當在主UI線程上執行某些其他操作時,進程對話框不會優先。只有在後臺完成操作時纔會使用它們。 doInBackground中的runonUIthread不會幫助你。這是progressdialog僅在幾秒鐘內纔可見的正常行爲。

+0

然後任何其他解決方案? – User42590

+0

我有同樣的情況,還沒有找到任何解決方案。對不起。 –

+0

@Akhter:你爲什麼不調用updateMapWithResult();在onPostExecute裏面? –

1

AsyncTask類中有兩個doInBackground()方法。從第一個doInBackground()中刪除runOnUiThread(),並將其移至第二個doInBackground(),其中有@Override註釋。

我不知道你是否想要寫兩個doInBackground()方法或者是錯誤的,但在方法之間有這樣的混淆是不好的。您的AsyncTask未調用第一個doInBackground(),它將調用doInBackground()其中有@Override註釋。所以你的ProgressDialog在1秒內被解僱,因爲它立即返回null。

+0

我做到了,但對話框僅在1毫秒內分散 – User42590