【问题标题】:Content Adapter and Async Callbacks内容适配器和异步回调
【发布时间】:2013-02-28 01:45:37
【问题描述】:

我正在使用第三方云服务 (Kinvey) 为我的应用获取数据。所有服务的 Kinvey 方法都包装在异步类中。我目前在下面收到错误日志,我明白为什么会收到它。我只是不知道如何解决这个问题。

错误:

03-12 13:41:03.449: E/AndroidRuntime(18375): FATAL EXCEPTION: main

03-12 13:41:03.449: E/AndroidRuntime(18375): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2130968661, class android.widget.ListView) with Adapter(class com.example.chartviewer.LazyAdapter)]


这是我在异步方法中删除内容后通知适配器的代码段:

mKinveyClient.appData("artistdata", ArtistData.class).delete(artistID, new KinveyDeleteCallback() {

       @Override
        public void onSuccess(KinveyDeleteResponse result) {

              Log.e("DELEET", "Data Deleted sucessfully");
              Toast.makeText(getBaseContext(),
                      "Artist Deleted", Toast.LENGTH_SHORT).show();

                //refreshes list 
            adapter.notifyDataSetChanged(); 

           //displays dialog box if no artists left 
           displayNoArtist();

        }
        @Override
        public void onFailure(Throwable error) {
             Log.e("DELETE", "AppData.delete Failure", error);
        }
    });


更新适配器:

mKinveyClient.appData("artistdata", ArtistData.class).get(myQuery, new 
KinveyListCallback<ArtistData>() {
         @Override
         public void onSuccess(ArtistData[] result) {

              Log.e("FETCHING", "Data fetched sucessfully");
             for (ArtistData data : result) {


                 String name= data.getArtistname();
                 String imageurl = data.getArtisturl();

                 String id = data.getArtistid();

                 artistIds.add(id);

                  HashMap<String, String> hashMap = new HashMap<String, String>();

                  hashMap.put(TAG_NAME, name);
                  hashMap.put(TAG_IMAGEURL, imageurl);

                  addMyArtists(hashMap);


             }

             displayData();

         }

         @Override
         public void onFailure(Throwable error) {

             Log.e("FETCHING", "AppData.get by Query Failure", error);

         }
     });


适配器创建者代码:

      list = (ListView)findViewById(R.id.artistlist);
      adapter = new LazyAdapter(MyArtistsActivity.this,"artists", artistItemList);
      list.setAdapter(adapter);
      registerForContextMenu(list);

【问题讨论】:

  • 真的取决于您在 AsyncTack 中调用更新的位置。您是从 onPostExecute 方法还是 doInBackground 方法调用?应始终通过 onPostExecute 方法调用 UI 更新。
  • ???没用过金维。您实际上是在哪里更新适配器的内容?
  • @G.BlakeMeike 上面添加的代码
  • 我是 Kinvey 的工程师。 appData.get 和 appData 删除方法被包装在一个 AsyncTask 中,回调 onSuccess 和 onFailure 在 onPostExecute 方法中被调用。 @AndroidEnthusiast - 你能发布你的适配器创建代码吗?
  • 另外 - 您是否在删除和更新方面都有问题,或者只是删除?

标签: android android-asynctask kinvey


【解决方案1】:

免责声明:我是 Kinvey 工程团队的成员。

问题是您在从 Kinvey 后端删除之前从 artistItemList 中删除,然后在回调之前不调用 adapter.notifyDatasetChanged。这会导致您在等待来自后端 Kinvey 删除调用的回调时注意到底层数据集发生变化的延迟。您需要将这两个调用组合在一起,并通过以下两种方式之一进行:

  1. 使您传递给 delete 方法的艺术家 ID 成为最终的,并在 onSuccess 中,在调用 adapter.notifyDatasetChanged 之前从 artistItemList 中删除该项目。
  2. 在调用 kinveyClient.appData().delete() 方法之前调用 adapter.notifyDatasetChanged,在从 ArtistItemList 中删除项目之后立即调用。如果在 Kinvey 后端删除失败,此选项可能会导致问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多