0

AsyncTask用来做后台处理,即我从服务器获取数据并存储在ListindoInBackground()方法中。我正在使用这个存储的数据onPostExecute()

我面临的问题是onPostExceute()甚至在执行doInBackground()完成之前就被执行。

有人可以建议可能出了什么问题吗?

代码:

AsyncTask<Void, Void, Void> read= new AsyncTask<Void, Void, Void>() {

        ProgressDialog progress= new ProgressDialog(LastData.this);
        ArrayList<String> testData;
        DBAdapter db;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progress.setTitle("Please Wait");
            testData= new ArrayList<String>();
            db= new DBAdapter(LastData.this);
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setMessage("Fetching Data !!");
            progress.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            ParseQuery query = new ParseQuery("Details");
            query.findInBackground(new FindCallback() {
                public void done(List<ParseObject> dataList,
                        ParseException e) {
                    if (e == null) {
                        Log.d("Details", "Retrieved " + dataList.size()
                                + " scores");
                        for (int idx = 0; idx < dataList.size(); idx++) {
                            data.add(dataList.get(idx).get("fname")
                                    .toString());
                        }
                    } else {
                        System.out.println(e.getMessage());
                    }

                    for (int idx = 0; idx < data.size(); idx++) {
                        testData.add(data.get(idx));
                    }

                    System.out.println("size inside is " + testData.size());
                }

            });

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progress.dismiss();

            Toast.makeText(LastData.this, "Size is "+testData.size(), Toast.LENGTH_LONG).show();
        }
    };

read.execute();

ToastonPostExecute()甚至在doInBackground方法执行之前就显示了 meassage in 。

4

2 回答 2

1

如果onPostExceute甚至在doInBackground完成任务之前执行,那么这意味着您可能正在asynchronous内部进行调用doInBackground。请发布代码以获得更具体的答案。

编辑:

问题是findInBackground异步运行,因此您不需要 AsynTask 来调用它。它已经可以在不阻塞您的线程的情况下运行,并且FindCallback会在找到某些内容时被调用。所以你需要把AsyncTask去掉,找到你调用的地方read.execute(...)替换掉,如下:

1)从那里 放代码 2)从onPreExecute()那里放代码doInBackground()
onPostExecuteFindCallback()

例子:

        /*...*/
        progress.setTitle("Please Wait");
        testData= new ArrayList<String>();
        db= new DBAdapter(LastData.this);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setMessage("Fetching Data !!");
        progress.show();
        ParseQuery query = new ParseQuery("Details");
        query.findInBackground(new FindCallback() {
            public void done(List<ParseObject> dataList,
                    ParseException e) {
                if (e == null) {
                    Log.d("Details", "Retrieved " + dataList.size()
                            + " scores");
                    for (int idx = 0; idx < dataList.size(); idx++) {

                        data.add(dataList.get(idx).get("fname")
                                .toString());
                    }

                } else {
                    System.out.println(e.getMessage());
                }


                for (int idx = 0; idx < data.size(); idx++) {

                    testData.add(data.get(idx));
                }
                System.out.println("size inside is " + testData.size());
                progress.dismiss();
                Toast.makeText(LastData.this, "Size is "+testData.size(), Toast.LENGTH_LONG).show();
            }

        });
        /*...*/
于 2012-06-26T15:31:47.820 回答
0

好像

query.findInBackground(new FindCallback() { ... }

是某种异步任务。我假设有一种方法

 query.find() 

这是同步的。

于 2012-06-26T15:59:25.443 回答