【问题标题】:AsyncTask, Insert, Select wrong order of execution in AndroidAndroid中的AsyncTask,Insert,选择错误的执行顺序
【发布时间】:2014-12-24 22:56:16
【问题描述】:

大家好,

我目前正在调试一个从数据库中检索某些数据的应用程序,之后它将在列表视图中显示它。但是,它使用了一种额外的方法来按日期对这些结果进行排序,并将其显示在名为“Gesorteerd op datum”的类别中,其中包含带有月份的子类别。示例:

Link screenshot because i miss 1 reputation to upload a image.

如您所见,打开的部分是静态的,我正在使用一个函数来选择某个时间段之间的所有数据,然后按月对这些数据进行排序,然后将它们插入数据库中,以使事情尽可能动态(我还插入了定制的“Gesorteerd op Datum”和月份)。然而,当第一次使用这个应用程序并打开这个片段时,“基准上的 Gesorteerd”部分完全丢失了。但是,通过按回然后再次进入它那里。

所以问题是插入需要更长的时间才能完成,然后选择和显示所有列表项的功能。我尝试使用 2 个 Asynctasks 来解决这个问题。

第一个 asynctask :DoInBackground 函数插入“Gesorteerd op datum”和其他子月,这使用了一个监听器,这样它在完成之前不会做下一件事情。然后 onPostExecute 运行一个显示类别的函数。但是这里已经出错了,因为“gesorteerd op datum”没有添加到稍后用于显示列表视图的数组中。

第二个异步任务:DoInBackground 使用一个函数,按日期获取和排序事件,然后插入它们。然后 onPostexecute 获取所有事件,包括“gesorteerd op datum”事件,并将它们显示在下面的列表视图中。如上所述,它没有做它应该做的事情。

谁知道哪里出错了?因为在阅读了 Asynctask 文档之后,我知道它不应该在第一次插入完成后加载类别。但它仍然会提前加载它们。

/*
 * Insert Static categories into database.
 * Easier to manipulate and prevents unnecessary code.
 */
class Static extends AsyncTask<Object, Object, Object>{     
    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub
        insertStaticData();
        return null;
    }

    @Override
    protected void onPostExecute(Object result)
    {
        getAllStaticData(); 
        new async().execute();
    }
}

/*
 * Insert Static categories into database.
 * Easier to manipulate and prevents unnecessary code.
 */
class async extends AsyncTask<Object, Object, Object>{      

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        toggleLoadingView(mLoading, mExpandableListView, mRootView, true);
    }

    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub
        if (sCurrentRadius != 0 && mLastLocation != null) {
            loadCursor(mLastLocation, sCurrentRadius); 
        } else{
            loadCursor();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object result)
    {
        getAllDynamicData();
    }
}

这些是我根据下面的建议编辑的异步。他们现在显示它们,但它不会显示它应该显示的所有月份。当我重新加载时,它会显示它们。

【问题讨论】:

  • 为什么不在初始 AsyncTask 的 onPostExecute 中选择数据,这样你就知道线程已经完成,然后再运行你的选择?
  • 你是什么意思?因为我的第一个首先创建静态类别并插入它们。然后在 onPostExecute 中它只获取类别。之后我开始第二个异步来获取和排序日期事件,然后在 onPostExecute 中显示类别中的事件。
  • 我明白你的意思。我试过了,但它根本不会显示。
  • 我现在得到了一个肮脏的解决方案,在执行第二个异步之前使用处理程序延迟 2 秒。这样插入有时间在代码继续之前完成。但是,这会使应用程序变慢一些,我希望有人能帮助我找到更清洁的解决方案

标签: android database select android-asynctask insert


【解决方案1】:

肮脏的解决方案:

创建一个处理程序并延迟 X 秒,以便完成插入。 X 可能会因您必须执行的插入量和操作量而异。

  /*
   * Run the Async and then after a delay load the listview needed. 
   */
  new Static().execute();

  final Handler handler = new Handler();
  handler.postDelayed(new Runnable() {
  @Override
         public void run() {
            if (sCurrentRadius != 0 && mLastLocation != null) {
                loadCursor(mLastLocation, sCurrentRadius); 
            } else{
                loadCursor();
            }
        }
    }, 4000);

/*
     * Insert Static categories into database.
     * Easier to manipulate and prevents unnecessary code.
     */
    class Static extends AsyncTask<Object, Object, Object>{     

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            toggleLoadingView(mLoading, mExpandableListView, mRootView, true);
        }

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

        @Override
        protected void onPostExecute(Object result)
        {
            getAllStaticData();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多