[英]Removing items from list not removing them from RecyclerView?
When I remove an itemfrom my list, it does not get removed from the RecyclerView at all. 当我从列表中删除项目时,它根本不会从RecyclerView中删除。
Here is how I remove ( user.getUid() does equal dataSnaphot.getkey() ): 这是我删除的方式( user.getUid()确实等于dataSnaphot.getkey() ):
for(User user: mFriendList){
if(user.getUid().equals(dataSnapshot.getKey())){
mFriendList.remove(user);
}
}
friendAdapter.notifyDataSetChanged();
My User object gets removed from the list but not from the view. 我的用户对象将从列表中删除,但不会从视图中删除。 When I use the same method for adding itemsto the list, like..
当我使用相同的方法添加项目列表时,像..
mFriendList.add(user);
friendAdapter.notifyDataSetChanged();
..it does get added to the list and view as well. ..它确实被添加到列表和视图中。 Why is it not working well with removing from the list?
为什么从列表中删除它不能正常工作?
EDIT: 编辑:
Setting up RecyclerView (inside a Fragment): 设置RecyclerView(在片段内):
mRecyclerView = (EmptyRecyclerView) rootView.findViewById(R.id.contactRecyclerView);
mRecyclerView.setHasFixedSize(true);
View myView = rootView.findViewById(R.id.emptyAdapterList);
mRecyclerView.setEmptyView(myView);
mLinearLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mFriendList = new ArrayList<User>();
friendAdapter = new FriendAdapter(mFriendList, getContext());
mRecyclerView.setAdapter(friendAdapter);
Removing items in foreach loop may cause undefined behavior depending on the collection. 删除foreach循环中的项可能会导致未定义的行为,具体取决于集合。
Instead of calling remove
method directly while iterating over collection You have four options: 在迭代集合时不直接调用
remove
方法您有四种选择:
final Iterator<User> iterator = mFriendList.iterator();
while(iterator.hasNext()) {
final User user = iterator.next();
if(user.getUid().equals(dataSnapshot.getKey())) {
iterator.remove();
}
}
final Collection toRemove = new ArrayList<User>();
for(User user: mFriendList){
if(user.getUid().equals(dataSnapshot.getKey())){
toRemove.add(user);
}
}
mFriendList.removeAll(toRemove);
mFriendList = mFriendList.stream()
.filter(user -> !user.getUid().equals(dataSnapshot.getKey()))
.collect(Collectors.toList());
mFriendList = Single.just(mFriendList)
.flatMapIterable(identity()) // list -> list
.filter(user -> !user.getUid().equals(dataSnapshot.getKey()))
.toList()
.blockingGet(); // or whatever transformation
Depends on whether you are using reactive approach or not I strongly recommend to use the latest one for consistency or just to remove dependencies from Stream Support libraries if you are targeting old enough Android SDK. 取决于您是否使用被动方法我强烈建议使用最新的方法来保持一致性,或者只是为了从流支持库中删除依赖项,如果您的目标是足够老的Android SDK。
Keep in mind you have to notifyDataSetChanged()
as soon as you will finish your modifications. 请记住,一旦完成修改,您必须立即
notifyDataSetChanged()
。
Given the limited info provided by your code my best guess would be that your removal fails since your data source is unaffected by your removed item from the list. 鉴于您的代码提供的信息有限,我最好的猜测是您的删除失败,因为您的数据源不受列表中已删除项目的影响。 If that's not it provide more code and I' wld be happy to help
如果不是它提供更多代码,我将很乐意提供帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.