我有一个RecyclerView,其中包含文件列表。用户可以点击许多RecyclerView行,然后上传所选文件。一旦文件被上传(在后台线程上),我想从RecyclerView中删除该行。
但是,我收到错误消息:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.next(ArrayList.java:860)
at UploadActivity$11$1.onFinish(UploadActivity.java:471)
at FileUploader.upload(FileUploader.java:115)
at UploadActivity$11.run(UploadActivity.java:458)
我了解我有多个线程同时访问和修改RecyclerView适配器,但是我不确定如何解决此问题(我的代码和尝试都发布在下面)。
首先,我循环浏览选定的RecyclerView项,获取每个选定的文件,然后调用upload()
// Loop through all selected RecyclerView items
for (int i = 0; i < selectedIndicies.size(); i++) {
// Get the i-th selected item
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
// Get the file associated with the i-th selected item
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
// Upload the file
uploadFile(file);
}
然后我开始一个新线程,开始上载,并定义onFinish()回调
public void uploadFile(SaveFile saveFile) {
...
new Thread() {
@Override
public void run() {
//
// Uploads the given file, when the upload is complete
// the onFinish() method is called and the file is passed
// back so I can update the RecyclerView
//
FileUploader.upload(saveFile, new FileUploader.FileUploadListener() {
@Override
public void onFinish(SaveFile file) {
// Loop through all items in RecyclerView
for (Upload_Item_Model item : adapter.getFilteredData()) { // this is line 471 where the crash happens
//
// If the RecyclerView item has the same name
// as the returned file, then it is
// the file I just uploaded
//
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
// Removes the item from the adapter
adapter.removeItem(item);
adapter.notifyDataSetChanged();
});
}
}
}
});
}
}.start();
}
在适配器中,我具有以下功能,用于访问和修改适配器ArrayList。我试图使这些线程安全无事。
public ArrayList<Upload_Item_Model> getFilteredData() {
synchronized (this.filteredData) {
return this.filteredData;
}
}
public void removeItem(Upload_Item_Model item) {
synchronized (this.filteredData) {
this.filteredData.remove(item);
}
}
任何帮助或建议都值得赞赏!
编辑+解决方案
我使用Rajat Mehra的解决方案使所有工作正常进行,该解决方案使用一个线程上载所有文件,而不是使用多个线程上载一个文件。为了使它正常工作,我确实需要进行一些小的调整,但是现在一切都顺利进行。
public void uploadFile() {
new Thread() {
@Override
public void run() {
for (int i = 0; i < selectedIndicies.size(); i++) {
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
FileUploader.upload(file, new FileUploader.FileUploadListener() {
@Override
public void onFinish() {
runOnUiThread(() -> {
// I can now simply use the selectedItem here!
adapter.removeItem(selectedItem);
adapter.notifyDataSetChanged();
});
}
});
}
}
}.start();
}
答案 0 :(得分:1)
创建一个线程并在其中一个接一个地上传所有文件,而不是创建多个线程。
public void uploadFile() {
new Thread() {
@Override
public void run() {
//
// Uploads the given file, when the upload is complete
// the onFinish() method is called and the file is passed
// back so I can update the RecyclerView
//
for (int i = 0; i < selectedIndicies.size(); i++) {
// Get the i-th selected item
Upload_Item_Model selectedItem = adapter.getFilteredData().get(selectedIndicies.get(i));
// Get the file associated with the i-th selected item
SaveFile file = getFileWithFilename(token, selectedItem.getTitle(), UploadActivity.this);
FileUploader.upload(file, new FileUploader.FileUploadListener() {
@Override
public void onFinish(SaveFile file) {
// Loop through all items in RecyclerView
for (Upload_Item_Model item : adapter.getFilteredData()) { // this is line 471 where the crash happens
//
// If the RecyclerView item has the same name
// as the returned file, then it is
// the file I just uploaded
//
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
// Removes the item from the adapter
adapter.removeItem(item);
adapter.notifyDataSetChanged();
});
}
}
}
});
}
}
}.start();
}
答案 1 :(得分:0)
调用adapter.notifyDataSetChanged();
时会引发异常,因为在notifyDataSetChanged
的后台处理了一些东西(没有同步),而同时在调用this.filteredData.remove(item);
时却没有不论是否包含同步关键字。
可能首先您需要致电所有removeItem
,然后致电adapter.notifyDataSetChanged();
像这样:
for (Upload_Item_Model item: adapter.getFilteredData()) {
if (item.getTitle().equals(file.getFilename())) {
runOnUiThread(() -> {
adapter.removeItem(item);
});
}
}
adapter.notifyDataSetChanged();
要考虑:notifyDataSetChanged()
不是更新回收者视图项目的有效方法。有DiffUtil.Callback
,可检查两个数据集之间的差异并有效地将更改分发到回收者视图。