簡體   English   中英

如何異步設置recyclerView適配器

[英]How to Set recyclerView adapter asynchronously

我編寫了一個類,用於在文件夾中查找特定文件,並將其地址(加上3個其他屬性)保存為共享首選項,然后在我的自定義適配器中調用方法(該方法(名為getAllItems )讀取共享的首選項中的數據,實例化新的自定義對象,並將它們添加到列表中並返回列表。這是發生最大負載的部分),然后適配器使用該項目列表從每個項目中檢索地址,生成位圖並在recyclerView中顯示它們,這是我得到的錯誤:

 E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
        Process: com.amir.example, PID: 4968
        java.util.ConcurrentModificationException
            at java.util.HashMap$HashIterator.nextEntry(HashMap.java:787)
            at java.util.HashMap$KeyIterator.next(HashMap.java:814)
            at com.android.internal.util.XmlUtils.writeSetXml(XmlUtils.java:355)
            at com.android.internal.util.XmlUtils.writeValueXml(XmlUtils.java:693)
            at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:300)
            at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:269)
            at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:235)
            at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:192)
            at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:600)
            at android.app.SharedPreferencesImpl.-wrap2(SharedPreferencesImpl.java)
            at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:515)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)

現在,當文件數很少時(例如100個文件中有20個匹配),應用程序可以正常工作,但是當文件太多(例如8000個文件中的200個項目)時,應用程序崩潰

我嘗試將項目異步加載到適配器中:這在MainActivity.java中

 public class AsyncAdapter extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {

        //This line will load the files into shared preferences (DataSource is my sharedPreferences)
        Loader.loadPhoneStickers(adapter.getDataSource()); 

        //this will cause the list that the adapter is attached to, to get updated and load all the items that were added to shared preferences 
        //this method will make a call to getAllItems and notifyItemRangeChanged
        adapter.refresh();
        return null;
    }
}

但是無論我異步設置適配器(設置適配器將導致所有加載發生)還是同步,錯誤仍然相同

我應該在哪里裝載? 奧托會有什么幫助嗎?

您將需要在asynctask內以及在ui線程上運行的postexecute上覆蓋此方法。 您無法從ui Thread刷新適配器。

 public class AsyncAdapter extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {

    //This line will load the files into shared preferences (DataSource is my sharedPreferences)
    Loader.loadPhoneStickers(adapter.getDataSource()); 

    //this will cause the list that the adapter is attached to, to get updated and load all the items that were added to shared preferences 
    //this method will make a call to getAllItems and notifyItemRangeChanged
    return null;
    }

 @Override
 protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

      adapter.refresh();
  }
}

通常,當您在其他線程中工作時,無法與ui元素進行交互。 通常的做法是在Asynctask中使用postExecute。doInbackground完成后立即調用post execute,並在UI線程上調用它,您可以在其中執行所有與Ui相關的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM