ProgressDialog不AsyncTask的示 [英] ProgressDialog not shown in AsyncTask

查看:156
本文介绍了ProgressDialog不AsyncTask的示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个SD卡一个庞大的数据库(40MB)。我需要获取数据,以 LIKE 查询,这是非常慢的。

I have a huge database (40MB) on an SDCard. I need fetch data, with LIKE in query, which is very slow.

DB请求大约需要5秒钟。因此,我需要异步做到这一点,与 ProgressDialog

DB request takes about 5 seconds. Therefore, I need to do it asynchronously and with ProgressDialog.

我用的AsyncTask 尝试过,但问题是与 ProgressDialog
它是这样实现:

I tried it with AsyncTask, but problem is with ProgressDialog.
It was implemented this way:

private class GetDataFromLangDB extends AsyncTask<String, String, String> {

    private final ProgressDialog dialog = new ProgressDialog(TranslAndActivity.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

         urDBCursor.close();
         curDBCursor = null;
         scaAdapter = null;

         this.dialog.setMessage("Loading data...");
         this.dialog.show();

    } 

    @Override
    protected String doInBackground(String... whatSearch) {
        String result = "";

        if (myDatabaseAdapter != null) {
            curDBCursor = myDatabaseAdapter.fetchAll(whatSearch[0]);
        }
        return result;
    }

    @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }

            prepareListView();
    }
}

问题是, ProgressDialog 未在DB请求过程中。
成品数据库查询后,将其在屏幕上闪烁的时间很短。当用户试图
挖掘数据库请求期间的屏幕,UI被冻结,并DB请求后
显示关于没有响应的消息。


我用一个线程尝试这种方式:

The problem is that ProgressDialog is not shown during the DB request. After finished database query, it flash on screen for a short time. When user tries to tap on screen during database request, UI is freezed, and after DB request message about 'not responding' is shown.

I tried it with a thread this way:

public void startProgress(View view, final String aWhatSearch) {

        final ProgressDialog dialog = new ProgressDialog(MyActivity.this);
        if (curDBCursor != null){
            curDBCursor.close();
            curDBCursor = null;
        }

        dialog.setMessage("Loading data...");
        dialog.show();
        Runnable runnable = new Runnable() {
            public void run() {
                curDBCursor = myDatabaseAdapter.fetchAll(aWhatSearch);
                // dirty trick
                try {
                    Thread.sleep(250); // it must be here to show progress
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                handler.post(new Runnable() {
                    public void run() {
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                        prepareListView();
                    }
                });

            }
        };
        new Thread(runnable).start();
    }

的结果是一样的,但是当我用的伎俩与视频下载(250);
  ProgressDialog 是在数据库请求过程中显示。但它不旋转,
 它的外观DB请求期间冻结。

The result was the same, but when I used the trick with Thread.sleep(250); ProgressDialog was shown during the database request. But it is not spinning, it looks freezed during the DB request.

DB的东西被称为这样(在搜索按钮自来水后):

DB stuff is called this way (after tap on search button):

btnSearchAll.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // AsyncTask
        new GetDataFromLangDB().execute(edtTextToSearch.getText().toString());

        // or Thread 
        //startProgress(null, edtTextToSearch.getText().toString());
    }
});

我发现了很多这样的问题,在SO,但没有什么是有用的我。
 难道说DB是SD卡?

I found a lot of problems like this in SO, but nothing was useful for me. Could it be that DB is on SD Card?

推荐答案

我把对话框进入的AsyncTask类的定义,它为我工作得很好。
看看这个exampel(你必须改变NAMEOFCLASS在你的类的名称:

I put the definition of the dialog into the AsyncTask Class and it works fine for me. Take a look at this exampel (You have to change NAMEOFCLASS in the name of your CLASS:

private class doInBackground extends AsyncTask<Integer, Integer, Void> {

   final ProgressDialog dialog = new ProgressDialog(NAMEOFCLASS.this) {

    @Override
    protected void onPreExecute() {
        dialog.setCancelable(false);
        dialog.setTitle(getString(R.string.daten_wait_titel));
        dialog.setIcon(R.drawable.icon);
        dialog.setMessage(getString(R.string.dse_dialog_speichern));
        dialog.show();
    }

    @Override
    protected void onCancelled() {
        dialog.cancel();
    }

...

    @Override
    protected void onProgressUpdate(Integer... values) {
                   // DO YOUR UPDATE HERE
    }

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

}

这篇关于ProgressDialog不AsyncTask的示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆