我正在使用第三方云服务(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);

最佳答案

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

问题是您要先从artistItemList中删除,然后再从Kinvey后端中删除,然后才在回调之前不调用adapter.notifyDatasetChanged。这会导致您在等待后端Kinvey delete调用的回调时注意到基础数据集发生了更改的滞后时间。您需要将这两个调用组合在一起,并以以下两种方式之一进行:


将传递给delete方法的artistID设置为final,然后在onSuccess中,在调用adapter.notifyDatasetChanged之前从artistItemList中删除该项目。
从artistItemList中删除项目后,立即调用adapter.notifyDatasetChanged,然后再调用kinveyClient.appData()。delete()方法。如果在Kinvey后端删除失败,此选项可能会导致问题。

关于android - 内容适配器和异步回调,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15363045/

10-10 23:09