-2

我正在尝试使用 ProgressDialog。当我运行我的应用程序时,进度对话框显示并在 1 秒后消失。我想在我的过程完成时展示它。这是我的代码:

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() 方法。

4

2 回答 2

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 对您没有帮助。这是进度对话框仅在几秒钟内可见的正常行为。

于 2013-01-18T07:42:39.653 回答
1

你的类中有两种doInBackground()方法AsyncTaskrunOnUiThread()从 First中删除doInBackground()并将其移动到doInBackground()具有@Override注释的第二个。

我不知道你是想写两个doInBackground()方法还是写错了,但是在方法之间有这样的混淆是不好的。你AsyncTask没有调用第一个doInBackground(),它会调用doInBackground()@Override注释的。因此,您ProgressDialog会在 1 秒内被解雇,因为它会立即返回 null。

于 2013-01-18T07:44:36.883 回答